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?
Asked
Active
Viewed 2.3k times
32
-
1An example might help: `%i( one two three ) #=> [:one, :two, :three]` – Sagar Pandya Oct 01 '16 at 05:42
2 Answers
52
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.
Andres
- 1,841
- 3
- 21
- 38
13
I'm having trouble finding what the
%idoes 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.
Jörg W Mittag
- 351,196
- 74
- 424
- 630
-
Some useful examples here: https://simpleror.wordpress.com/2009/03/15/q-q-w-w-x-r-s/ – Mark Oct 03 '16 at 17:00
-
2So %i and %I are almost the same thing as %w and %W. Difference between "interpolated" and 'non-interpolated' is not obvious either, but it's the difference between double-quotes (which interpolate \n and #{...} items) and single-quotes (which do not) – Mark Oct 03 '16 at 17:06