Ruby Quick Reference

Quick reference for efficient coding

View the Project on GitHub YumaYX/RubyQuickReference

percent_expression.rb(array)

This Ruby code demonstrates different ways to create arrays:

  1. %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 variable str.

  2. %W(a b c "#{str}"): Similar to the previous one but allows interpolation and double-quoted strings.

  3. %i(a b c "#{str}"): Creates an array of symbols, where each element is a symbol, and interpolation is not performed.

  4. %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.5