Fralcon
Fralcon

Reputation: 489

What is the benefit of using % notation?

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

Answers (2)

Nucc
Nucc

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

Sergio Tulentsev
Sergio Tulentsev

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

Related Questions