Ruby % Notation
For a long time, my Rubocop always kept complaining about the list of symbols defined in my code and, I also didn’t search for the reason behind it. It states that:
Prefer %i to the literal array syntax when you need an array of symbols (and you don’t need to maintain Ruby 1.9 compatibility). Apply this rule only to arrays with two or more elements.
So after some searching, I’ve found that’s pretty common and, generally, more readable for the developers to use this notation. So here goes a brief resume and examples about them:
# Yes, they are case sensitive
%q[ ] # Non-interpolated String (except for \\ \[ and \])
%Q[ ] # Interpolated String (default)
%r[ ] # Interpolated Regexp (flags can appear after the closing delimiter)
%i[ ] # Non-interpolated Array of symbols, separated by whitespace
%I[ ] # Interpolated Array of symbols, separated by whitespace
%w[ ] # Non-interpolated Array of words, separated by whitespace
%W[ ] # Interpolated Array of words, separated by whitespace
%x[ ] # Interpolated shell command
%q
%q{one\ntwo\n#{ 1 + 2 }}
# => "one\\ntwo\\n\#{ 1 + 2 }"
%Q
%Q{one\ntwo\n#{ 1 + 2 }}
# => "one\ntwo\n3"
%r
name = 'nemo'
%r/#{name}/i
# => /nemo/i
%i
%i[one two three] # after Ruby 2.0
# => [:one, :two, :three]
%I
value = 'searching'
%I[#{value} for nemo]
# => [:searching, :for, :nemo]
%w
%w[one two three]
# => ["one", "two", "three"]
%W
value = 'one'
%W[#{value} two three]
# => ["one", "two", "three"]
%x
%x{ruby --copyright}
# => "ruby - Copyright (C) 1993-2020 Yukihiro Matsumoto\n"