Skip to content

Instantly share code, notes, and snippets.

@jenny-codes
Last active April 1, 2019 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenny-codes/60847c96eac91a2bf13e59cc8d06564a to your computer and use it in GitHub Desktop.
Save jenny-codes/60847c96eac91a2bf13e59cc8d06564a to your computer and use it in GitHub Desktop.
# 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