Ruby arrays: %w vs %W
What is the difference?
Solution 1:
%w quotes like single quotes ''
(no variable interpolation, fewer escape sequences), while %W quotes like double quotes ""
.
irb(main):001:0> foo="hello"
=> "hello"
irb(main):002:0> %W(foo bar baz #{foo})
=> ["foo", "bar", "baz", "hello"]
irb(main):003:0> %w(foo bar baz #{foo})
=> ["foo", "bar", "baz", "\#{foo}"]
Solution 2:
An application I've found for %W vs %w:
greetings = %W(hi hello #{"how do you do"})
# => ["hi", "hello", "how do you do"]
Solution 3:
%W performs normal double quote substitutions. %w does not.
Solution 4:
Though an old post, the question keep coming up and the answers don't always seem clear to me. So, here's my thoughts.
%w and %W are examples of General Delimited Input types, that relate to Arrays. There are other types that include %q, %Q, %r, %x and %i.
The difference between upper and lower case is that it gives us access to the features of single and double quote. With single quotes and lowercase %w, we have no code interpolation (e.g. #{someCode} ) and a limited range of escape characters that work (e.g. \, \n ). With double quotes and uppercase %W we do have access to these features.
The delimiter used can be any character, not just the open parenthesis. Play with the examples above to see that in effect.
For a full write up with examples of %w and the full list, escape characters and delimiters - have a look at: http://cyreath.blogspot.com/2014/05/ruby-w-vs-w-secrets-revealed.html
Mark
Solution 5:
Documentation for Percent Strings: http://ruby-doc.org/core-2.2.0/doc/syntax/literals_rdoc.html#label-Percent+Strings