All Questions

417
votes
13answers
98381 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?
258
votes
18answers
61403 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?
199
votes
11answers
83508 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.de...
801
votes
19answers
348707 views

What is attr_accessor in Ruby?

I am having a hard time understanding attr_accessor in Ruby. Can someone explain this to me?
747
votes
6answers
220941 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?
274
votes
5answers
67442 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 ?
59
votes
3answers
8492 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 worl...
80
votes
5answers
15775 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 arr...
276
votes
2answers
78034 views

Ruby ampersand colon shortcut

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, &a...
801
votes
18answers
405349 views

Calling 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?

Previous Next