What are semantics of %Q and %{} in Ruby? What is difference between them?
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] %?
The usage of "%I" is just to create hash keys from an array of strings, separated by whitespaces.
%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 .
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.
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).
There are in effect the same. Both follow double-quoted string semantics
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With