Pattern Matching
Array Patterns
Array patterns destructure positions into named variables.
Array Patterns
array_patterns.rb
amount =
command = ["add", amount]
case command
in ["add", value]
result = value + 10
in ["remove", value]
result = 10 - value
else
result = 0
end
puts "command=#{command.join(",")}"
puts "result=#{result}"
amount =
command = ["add", amount]
case command
in ["add", value]
result = value + 10
in ["remove", value]
result = 10 - value
else
result = 0
end
puts "command=#{command.join(",")}"
puts "result=#{result}"
amount =
command = ["add", amount]
case command
in ["add", value]
result = value + 10
in ["remove", value]
result = 10 - value
else
result = 0
end
puts "command=#{command.join(",")}"
puts "result=#{result}"
array pattern
An array pattern can match shape and assign elements to local variables.