Ruby% () vs ""

I noticed that some people use% (line here) instead of just using double quotes as "line here". Is there a reason for this? When I use the first layout, I usually make an array such as% w (my array is here), so I don't need to use quotes and commas.

Is there a hidden rule that I don't know about? I cannot imagine why I would do this:

a = %(some string here)

instead

b = "some string here"

The latter seems more clearly written.

+4
source share
2 answers

They are almost equivalent, using %()you do not need to hide the character "inside the string:

s = %(foo "bar" baz)
# => "foo \"bar\" baz"

They are mostly useful when your string is filled with double quotes.

+6

, , %(), . .

0

All Articles