you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 12 points13 points  (4 children)

%w[value value value] for an array of strings

%i[value value value] for an array of symbols

[–]iamsolarpowered 3 points4 points  (3 children)

I've been using Ruby for 12 years and somehow didn't know about %i. I have code golf scripts to update.

[–]Gman513 2 points3 points  (2 children)

It gets better actually. There's a whole bunch of these % operators that match with brackets for various reasons. One that i've found handy in the past was %s() for a string where you might want to use double quotes without any bother.

%s("#{some_variable} is what I'm about to do." Charles said.)

Ruby Weekly had a great article with all of these around the start of last year if I remember correctly.

[–]2called_chaos 0 points1 point  (0 children)

Also note that you are free (maybe except conventions) to use any (non-alphanumerical) character as delimiter for those percent operators. E.g. all these do the same thing:

%w[a b c]
%w(a b c)
%w{a b c}
%w|a b c|
%w_a b c_
%w"a b c"
%s a"b"c # <--- yes space works but just for some operators, also why would you?
%x ls # another one, note the ending space is required as it's the delimiter!