What does %i or %I do in Ruby?
%i[ ] # Non-interpolated Array of symbols, separated by whitespace
%I[ ] # Interpolated Array of symbols, separated by whitespace
The second link from my search results http://ruby.zigzo.com/2014/08/21/rubys-notation/
Examples in IRB:
%i[ test ]
# => [:test]
str = "other"
%I[ test_#{str} ]
# => [:test_other]
It can be hard to find the official Ruby documentation (it's here). At the time of writing the current version is 2.5.1, and the documentation for the %i construct is found in the documentation for Ruby's literals.
There are some surprising (to me at least!) variants of Ruby's % construct. There are the often used %i %q %r %s %w %x
forms, each with an uppercase version to enable interpolation. (see the Ruby literals docs for explanations.
But you can use many types of delimiters, not just []
. You can use any kind of bracket () {} [] <>
, and you can use (quoting from the ruby docs) "most other non-alphanumeric characters for percent string delimiters such as “%”, “|”, “^”, etc."
So %i% bish bash bosh %
works the same as %i[bish bash bosh]