This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OUTPUT