Skip to content

Comment on Github Ruby Styleguide

Comments

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.

I'm sure this would only apply to an array with single word strings. If you need strings with spaces, %w definitely isn't appropriate.

> 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.

Ahh.. I forgot about the quotes. That's what I get for posting immediately after waking up. Thanks :)

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.