Compiled from this Stack Overflow answer, here is the list of Ruby’s very useful %
operators:
-
%w()
creates an array from space-delimited strings (a b c
becomes[‘a’, ‘b’, ‘c’]
) -
%r()
is another way to write a regular expression -
%q()
is for single-quoted strings (so you can do%q(that's right)
without escaping the'
-
%Q()
is for double-quoted strings so you don't have to escape"
, but can do interpolation (%Q(#{1+1}) => "2"
) -
%x()
is a shell command -
%i()
gives an array of symbols (Ruby >= 2.0.0) -
%s()
turnsfoo
into a symbol (:foo
)