Making arrays with ruby, without those pesky commas

The most common way to create an array in Ruby is do this:

my_shiny_new_array = ["tom", "dick", "harry", "jonah", "percy", "jermaine"]

Thanks to its perly origins though, you also have a more idiomatic way to do this in Ruby, like so:

%w(tom dick harry jonah percy jermaine)

Because Ruby has such a flexible syntax, it's easy to forget that when you use some of it's more magical tricks like wrapping a series of words in %w() to create an array, you're really calling a method called %w, and passing in the the strings as params like any other method.

This trick works with other delimiting characters too, using {} or [] like below gives the same result.

# all these create an array in the same way
%w(tom dick harry jonah percy jermaine)
%w[tom dick harry jonah percy jermaine]
%w{tom dick harry jonah percy jermaine}

This post has been updated since I managed to forgot to wrap the strings in quotes, and it was pointed out by a commenter (thanks Jakub!)



Copyright © 2020 Chris Adams
Powered by Cryogen
Theme by KingMob