Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# method definition
def run_variations(variation_options = {}, &block)
keys = variation_options.keys
values = variation_options.fetch_values(*keys)
variant_klass = Struct.new(*keys)
values.first
.product(*values[1..-1])
.each do |variation|
block.call(variant_klass.new(*variation))
end
end
# calling the method
run_variations(set: ['蛋炒飯', '紅蘿蔔蛋炒飯'],
side: ['皮蛋豆腐', '涼拌乾絲', '滷味拼盤'],
with_sauce: [true, false]) do |combination|
puts "[Menu option] set: #{combination.set}, side: #{combination.side}, with_sauce: #{combination.with_sauce}"
end
@jenny-codes
Copy link
Author

jenny-codes commented Apr 1, 2019

OUTPUT

[Menu option] set: 蛋炒飯, side: 皮蛋豆腐, with_sauce: true
[Menu option] set: 蛋炒飯, side: 皮蛋豆腐, with_sauce: false
[Menu option] set: 蛋炒飯, side: 涼拌乾絲, with_sauce: true
[Menu option] set: 蛋炒飯, side: 涼拌乾絲, with_sauce: false
[Menu option] set: 蛋炒飯, side: 滷味拼盤, with_sauce: true
[Menu option] set: 蛋炒飯, side: 滷味拼盤, with_sauce: false
[Menu option] set: 紅蘿蔔蛋炒飯, side: 皮蛋豆腐, with_sauce: true
[Menu option] set: 紅蘿蔔蛋炒飯, side: 皮蛋豆腐, with_sauce: false
[Menu option] set: 紅蘿蔔蛋炒飯, side: 涼拌乾絲, with_sauce: true
[Menu option] set: 紅蘿蔔蛋炒飯, side: 涼拌乾絲, with_sauce: false
[Menu option] set: 紅蘿蔔蛋炒飯, side: 滷味拼盤, with_sauce: true
[Menu option] set: 紅蘿蔔蛋炒飯, side: 滷味拼盤, with_sauce: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment