Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [ruby]

Ruby is a multi-platform open-source, dynamic object-oriented interpreted language. The [ruby] tag is for questions related to the Ruby language, including its syntax and its libraries. Ruby on Rails questions should be tagged with [ruby-on-rails].

413 votes
7 answers
123k views

What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Can some one please describe the usage of the following characters which is used in ERB file: <% %> <%= %> <% -%> <%# %> what's the usage of each one ?
simo's user avatar
  • 24.2k
556 votes
17 answers
157k views

What does map(&:name) mean in Ruby?

I found this code in a RailsCast: def tag_names @tag_names || tags.map(&:name).join(' ') end What does the (&:name) in map(&:name) mean?
collimarco's user avatar
120 votes
5 answers
19k views

Strange, unexpected behavior (disappearing/changing values) when using Hash default value, e.g. Hash.new([])

Consider this code: h = Hash.new(0) # New hash pairs will by default have 0 as values h[1] += 1 #=> {1=>1} h[2] += 2 #=> {2=>2} That’s all fine, but: h = Hash.new([]) # Empty array ...
Valentin V's user avatar
  • 25.6k
286 votes
14 answers
145k views

Is Ruby pass by reference or by value?

@user.update_languages(params[:language][:language1], params[:language][:language2], params[:language][:language3]) lang_errors = @user.errors logger....
Sid's user avatar
  • 6,234
1155 votes
20 answers
551k views

What is attr_accessor in Ruby?

I am having a hard time understanding attr_accessor in Ruby. Can someone explain this to me?
dennismonsewicz's user avatar
78 votes
3 answers
10k views

Why do Ruby setters need "self." qualification within the class?

Ruby setters—whether created by (c)attr_accessor or manually—seem to be the only methods that need self. qualification when accessed within the class itself. This seems to put Ruby alone the world of ...
Purfideas's user avatar
  • 3,288
985 votes
6 answers
355k views

Why is it bad style to `rescue Exception => e` in Ruby?

Ryan Davis’s Ruby QuickRef says (without explanation): Don’t rescue Exception. EVER. or I will stab you. Why not? What’s the right thing to do?
John's user avatar
  • 29.8k
852 votes
12 answers
871k views

Execute a command line binary with Node.js

I am in the process of porting a CLI library from Ruby over to Node.js. In my code I execute several third party binaries when necessary. I am not sure how best to accomplish this in Node. Here's an ...
Dave Thompson's user avatar
460 votes
22 answers
191k views

What does ||= (or-equals) mean in Ruby?

What does the following code mean in Ruby? ||= Does it have any meaning or reason for the syntax?
collimarco's user avatar
1266 votes
22 answers
651k views

How to call shell commands from Ruby

How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby?
CodingWithoutComments's user avatar
342 votes
2 answers
105k views

Ruby ampersand colon shortcut [duplicate]

Possible Duplicate: What does map(&:name) mean in Ruby? In Ruby, I know that if I do: some_objects.each(&:foo) It's the same as some_objects.each { |obj| obj.foo } That is, &:foo ...
Allan Grant's user avatar
  • 3,849
143 votes
5 answers
32k views

Is there any difference between the `:key => "value"` and `key: "value"` hash notations?

Is there any difference between :key => "value" (hashrocket) and key: "value" (Ruby 1.9) notations? If not, then I would like to use key: "value" notation. Is there a gem that helps me to convert ...
AdamNYC's user avatar
  • 20.2k
502 votes
3 answers
102k views

When monkey patching an instance method, can you call the overridden method from the new implementation?

Say I am monkey patching a method in a class, how could I call the overridden method from the overriding method? I.e. Something a bit like super E.g. class Foo def bar() "Hello" end end ...
James Hollingworth's user avatar
34 votes
4 answers
23k views

How to dynamically create a local variable?

I have a variable var = "some_name" and I would like to create a new object and assign it to some_name. How can I do it? E.g. var = "some_name" some_name = Struct.new(:name) # I need this a = ...
Bala's user avatar
  • 11.1k
44 votes
3 answers
15k views

Why is "slurping" a file not a good practice?

Why is "slurping" a file not a good practice for normal text-file I/O, and when is it useful? For example, why shouldn't I use these? File.read('/path/to/text.txt').lines.each do |line| # do ...
the Tin Man's user avatar

15 30 50 per page
1
2 3 4 5
1354