Reading through a list of Rails questions, I'm having trouble finding what the %i does in relation to a symbol array. Does this mean anything to anyone?
Save this answer. Show activity on this post. %w(foo bar) is a shortcut for ["foo", "bar"] . Meaning it's a notation to write an array of strings separated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider's quickref.
The usage of "%I" is just to create hash keys from an array of strings, separated by whitespaces.
A literal is a special syntax in the Ruby language that creates an object of a specific type. For example, 23 is a literal that creates a Fixnum object. As for String literals, there are several forms.
Lowercase %i
stands for
Non-interpolated Array of symbols, separated by whitespace (after Ruby 2.0)
In addition, uppercase %I
means
Interpolated Array of symbols, separated by whitespace (after Ruby 2.0)
Example of difference regarding interpolation:
2.4.2 :001 > a = 1 2.4.2 :002 > %i{one two #{a}+three} # Interpolation is ignored => [:one, :two, :"\#{a}+three"] 2.4.2 :003 > %I{one two #{a}+three} # Interpolation works => [:one, :two, :"1+three"]
Have a look at here for further information.
I'm having trouble finding what the
%i
does in relation to a symbol array.
It is an array literal for an array of symbols. It does the same thing in relation to symbol arrays as '
does to strings.
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