Ruby arrays: %w vs %W

Viewed 43911

What is the difference?

7 Answers

%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}"]

%W performs normal double quote substitutions. %w does not.

array = %w(a b c d) 

Same As

array = ["a", "b", "c", "d"]

%w is a short cut symbol for the quotation mark to the string!

Related