36

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?

Mark
  • 660
  • 1
  • 6
  • 9

2 Answers2

58

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
  • 2,099
  • 3
  • 22
  • 39
15

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.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 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
  • 2
    So %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