26 June 2014

When declaring an array of string, rather than declaring it literally as:

["apple", "orange"]

We can do it as:

%w(apple orange)

This saves typing the quote and comma. This is pretty well known.

What may be less known is that there is a difference between using %w() and %W(). The big one enables interpolation. For example

%w( #{1+1} )  #=> [ "\#{1+1}" ]
%W( #{1+1} )  #=> [ "2" ]

Also, we know ruby don’t have character as variable type, it has only string. But you can declare a one character long string by using

?z  #=> "z"

This is sometimes useful in case statements (and saves one keystroke, woohoo).