Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between %Q and %{} in ruby?

Tags:

ruby

What are semantics of %Q and %{} in Ruby? What is difference between them?

like image 801
Jey Geethan Avatar asked Aug 13 '09 18:08

Jey Geethan


People also ask

What is %{} in Ruby?

This is percent sign notation. The percent sign indicates that the next character is a literal delimiter, and you can use any (non alphanumeric) one you want. For example: %{stuff} %[stuff] %?

What does %I mean in Ruby?

The usage of "%I" is just to create hash keys from an array of strings, separated by whitespaces.

What is %W in Ruby on Rails?

%w[hello billing confirmation] is syntax sugar for ["hello", "billing", "confirmation"] . It tells Ruby to break up the input string into words, based on the whitespace, and to return an array of the words. If your specific use case means the values in the array are permitted to have spaces, you cannot use %w .


3 Answers

Jim Hoskins clears it up.

%Q is the equivalent to a double-quoted ruby string. #{expression} evaluation works just like in double-quoted strings, even if you use %Q{} as your delimiter!

You can also leave off the Q and it will have the same functionality. I recommend leaving the Q in to be more clear.

like image 186
mcandre Avatar answered Nov 12 '22 17:11

mcandre


No, there is no functional difference. Some might argue that %Q{} is a little clearer, but both are interpolated strings (just like using double quotes).

like image 24
John Hyland Avatar answered Nov 12 '22 17:11

John Hyland


There are in effect the same. Both follow double-quoted string semantics

like image 22
ennuikiller Avatar answered Nov 12 '22 18:11

ennuikiller