dingalingchickenwiing
dingalingchickenwiing

Reputation: 1997

Ruby %() vs. ""

I notice some people use %(string here) instead of a simple use of double quotes as "string here". Is there any reason for this? When I use the first layout, I usually make an array such as %w(my array here) so I don't have to use quotes and commas.

Is there a hidden rule I am unaware of? I can't imagine why I would do this:

a = %(some string here)

instead of

b = "some string here"

The latter just seems more clearly written.

Upvotes: 7

Views: 2542

Answers (2)

toro2k
toro2k

Reputation: 19228

They are almost equivalent, using %() you don't have to escape the " character inside the string:

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

They are mostly useful when your string is full of double quotes.

Documentation

Upvotes: 9

Bob Gilmore
Bob Gilmore

Reputation: 13758

If you're going to have double-quotes embedded within the the string itself, it can be easier to do the %() than to properly escape all of the double-quotes. It may be more readable, too.

Upvotes: 0

Related Questions