Reputation: 489
I see literal notation like %w[foo bar]
being used all the time, but I dont see the benefit in using that over ["foo", "bar"]
. Is it used as convention, or is this some performance/time benefit?
Upvotes: 0
Views: 72
Reputation: 1031
No, there's no any performance benefit. Sometimes it's easier and looks better if you use this. Easier to write and more simple %(One Two Three Four)
is, that's all...
Upvotes: 0
Reputation: 230346
In some cases it's more readable. Compare these made-up examples:
%w(the quick brown fox jumped over the lazy dog)
["the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"]
"the quick brown fox jumped over the lazy dog".split(' ')
While the third one is almost as readable as the first one, it performs worse (needs to allocate and populate array). The first two, on the other hand, are processed at parse time.
Upvotes: 1