170

What is the difference?

Allyn
  • 20,271
  • 16
  • 57
  • 68

7 Answers7

265

%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}"]
Gavin S. Yancey
  • 1,216
  • 1
  • 13
  • 34
Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • Your example show double quotes for %w too. Is it correct? – Foton Dec 03 '16 at 14:02
  • 1
    Yes. When printing output, Ruby always uses double quotes and escapes characters like `#`. `'#{foo}'` and `"\#{foo}"` give you the same string, which you can verify with `'#{foo}' == "\#{foo}"` in `irb`. – Brian Campbell Dec 04 '16 at 05:37
  • It's little confusing on first see, but thanks for explanation. – Foton Dec 05 '16 at 08:40
29

An application I've found for %W vs %w:

greetings = %W(hi hello #{"how do you do"})
# => ["hi", "hello", "how do you do"]
aaandre
  • 2,502
  • 5
  • 33
  • 46
  • 12
    Although in this case, it's prob easier to just do `greetings = %w(hi hello how\ do\ you\ do)` – dinjas Feb 11 '14 at 23:17
21

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

Brian
  • 4,931
  • 3
  • 32
  • 55
12

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

Mark C
  • 615
  • 9
  • 15
  • 1
    That's a good article. I didn't realize you can make strings or other things the same way and the enclosing characters can be whatever you want. For example, `%w&readable af&` – Dex Jun 29 '19 at 07:03
3

Documentation for Percent Strings: http://ruby-doc.org/core-2.2.0/doc/syntax/literals_rdoc.html#label-Percent+Strings

itsnikolay
  • 17,415
  • 4
  • 65
  • 64
2

%W is used for double-quoted array elements like %Q, for example,

foo = "!"
%W{hello world #{foo}} # => ["hello", "world", "!"]

%w is used for single-quoted array elements like %q.

%w(hello world #{foo})
# => ["hello","world", "\#{foo}"]
Timo Schilling
  • 3,003
  • 1
  • 17
  • 29
Faruk Hossen
  • 185
  • 1
  • 8
0
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!

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Kasem777
  • 737
  • 7
  • 10