Quote by Whitespace in Ruby
Coming from a Perl background, I love Perl’s quote by whitespace syntax:
#!/usr/bin/perl
use strict;
my @words = qw( this is an array of words );
print "$words[3]\n";
# => array
So I wondered: Does Ruby have an equivalent of Perl’s qw
? It turns out, it does! To “quote by whitespace” in Ruby:
array = "ARRAY"
words = %w( this is an #{array} of words )
puts words[3]
# => #{array}
more_words = %W( this is an #{array} of words )
puts more_words[3]
# => ARRAY
Note that %w
creates an array of single-quoted strings, whereas %W
creates an array of double-quoted strings, allowing for string interpolation.