Heads up: This description was created by AI and might not be 100% accurate.
percent_expression.rb
This Ruby code snippet demonstrates string and symbol array creation using different shortcuts.
%w()
creates an array of strings by splitting the input string at whitespace. String interpolation (#{str}
) is not evaluated within%w
.%W()
is similar to%w()
, but does evaluate string interpolation, creating an array of strings.%i()
creates an array of symbols. String interpolation is treated literally.%I()
creates an array of symbols and does evaluate string interpolation, creating symbols from the evaluated strings.
Ruby code snippet
str='ruby'
#=> "ruby"
%w(a b c "#{str}")
#=> ["a", "b", "c", "\"\#{str}\""]
%W(a b c "#{str}")
#=> ["a", "b", "c", "\"ruby\""]
%i(a b c "#{str}")
#=> [:a, :b, :c, :"\"\#{str}\""]
%I(a b c "#{str}")
#=> [:a, :b, :c, :"\"ruby\""]
Executed with Ruby 3.4.5
.