Ruby Proc Syntax

When you prefix the last argument of a call with & you are making clear that you are sending a block and not a normal argument. Ok, in method(&:something), :something is a symbol, not a proc, so Ruby automatically calls the method to_proc to get a real block. And Rails guys (and now also vanilla … Read more

Where is Ruby’s string literal juxtaposition feature officially documented?

UPDATE This is now officially documented in the RDoc that ships with Ruby. Changes will propagate to RubyDoc the next time they build the documentation. The added documentation: Adjacent string literals are automatically concatenated by the interpreter: “con” “cat” “en” “at” “ion” #=> “concatenation” “This string contains “\ “no newlines.” #=> “This string contains no … Read more

Is sort in Ruby stable?

Both MRI‘s sort and sort_by are unstable. Some time ago there was a request to make them stable, but it was rejected. The reason: Ruby uses an in-place quicksort algorithm, which performs better if stability is not required. Note that you can still implement stable methods from unstable ones: module Enumerable def stable_sort sort_by.with_index { … Read more

Why does Ruby seem to hoist variable declarations from inside a case statement even if that code path is not executed? [duplicate]

When the Ruby parser sees the sequence identifier, equal-sign, value, as in this expression x = 1 it allocates space for a local variable called x. The creation of the variable—not the assignment of a value to it, but the internal creation of a variable—always takes place as a result of this kind of expression, … Read more

Weird backslash substitution in Ruby

Quick Answer If you want to sidestep all this confusion, use the much less confusing block syntax. Here is an example that replaces each backslash with 2 backslashes: “some\\path”.gsub(‘\\’) { ‘\\\\’ } Gruesome Details The problem is that when using sub (and gsub), without a block, ruby interprets special character sequences in the replacement parameter. … Read more

I don’t understand ruby local scope

There a couple of things going on here. First, variables declared inside the if block have the same local scope as variables declared at the top level of the method, which is why bar is available outside the if. Second, you’re getting that error because bob is being referenced straight out of the blue. The … Read more

Ruby multidimensional array

Strictly speaking it is not possible to create multi dimensional arrays in Ruby. But it is possible to put an array in another array, which is almost the same as a multi dimensional array. This is how you could create a 2D array in Ruby: a = [[1,2,3], [4,5,6], [7,8,9]] As stated in the comments, … Read more

How to merge Ruby hashes

There is a Hash#merge method: ruby-1.9.2 > a = {:car => {:color => “red”}} => {:car=>{:color=>”red”}} ruby-1.9.2 > b = {:car => {:speed => “100mph”}} => {:car=>{:speed=>”100mph”}} ruby-1.9.2 > a.merge(b) {|key, a_val, b_val| a_val.merge b_val } => {:car=>{:color=>”red”, :speed=>”100mph”}} You can create a recursive method if you need to merge nested hashes: def merge_recursively(a, b) … Read more