Heads up: This description was created by AI and might not be 100% accurate.
percent_expression.rb
This Ruby code snippet demonstrates the use of interpolation in arrays and hashes. In Ruby, you can include variables inside a string by using the #{}
notation. For example, if we have a variable str = "ruby"
, then we can create an array with the elements “a”, “b”, “c”, and “ruby” like this:
%w(a b c "#{str}")
=> ["a", "b", "c", "ruby"]
This creates an array with four elements, where the third element is the value of the variable str
.
Similarly, we can create a hash with the keys “a”, “b”, and “c” and the value of the variable str
like this:
%i(a b c "#{str}")
=> {:a=>1, :b=>2, :c=>3, str=>"ruby"}
This creates a hash with four key-value pairs, where the third key is the value of the variable str
. Note that the keys in this case are symbols, not 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.4
.