3 ms·
I'm a little curious why %w(strings go here) is preferred to [strings, go, here] The first form requires extra work to escape spaces, the second f
by Karunamon 14y ago
I'm a little curious why
%w(strings go here)
is preferred to
[strings, go, here]
The first form requires extra work to escape spaces, the second form is a representation of an array everywhere in Ruby, including in the debugger and IRB.
- danenania 14y agoI'm sure this would only apply to an array with single word strings. If you need strings with spaces, %w definitely isn't appropriate.
- masklinn 14y ago> The first form requires extra work to escape spaces The first form is generally used for symbol-type strings (word lists, essentially, hence "w"). It is less syntactically noisy, shorter and requires less waffling around with non-word keys. That's especially flagrant if you don't ignore half the actual array, %w(foo bar baz) really results in ["foo", "bar", "baz"] (the quotes are rather important, because they're noisy, repetitive and annoying to type). It's one of the few things I regularly wish were in more languages.
- Karunamon 14y agoAhh.. I forgot about the quotes. That's what I get for posting immediately after waking up. Thanks :)