percent_expression.rb(array)
This Ruby code demonstrates different ways to create arrays:
-
%w(a b c "#{str}")
: Creates an array of strings using whitespace as a delimiter, treating the elements as single-quoted strings except for the interpolation of the variablestr
. -
%W(a b c "#{str}")
: Similar to the previous one but allows interpolation and double-quoted strings. -
%i(a b c "#{str}")
: Creates an array of symbols, where each element is a symbol, and interpolation is not performed. -
%I(a b c "#{str}")
: Similar to the previous one but allows interpolation.
In summary, these variations provide flexibility in creating arrays with different types of elements and string interpolation.
Execution:
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.3.6