Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Created February 4, 2015 15:57
Show Gist options
  • Save Shinpeim/bae986401c10009096c1 to your computer and use it in GitHub Desktop.
Save Shinpeim/bae986401c10009096c1 to your computer and use it in GitHub Desktop.
class Bento
attr_reader :price
attr_writer :seals
def initialize(price)
@price = price
@seals = []
end
def discount_ammount
sell_price - @price
end
def sell_price
prices = @seals.map do |seal|
seal.call(@price)
end
prices.sort{|a, b| a <=> b}.first
end
def discount(seals)
bento = dup
bento.seals = seals
bento
end
def to_s
"原価: #{@price}, 値引き後: #{sell_price}, 値引額: #{discount_ammount}"
end
end
半額シール = ->(price){ price / 2 }
おつとめ品シール = ->(price) { 200 } # おつとめ品は一律200円
def セール(bento_list, seal_list)
bento_list.map do |bento|
bento.discount(seal_list)
end
end
弁当リスト = [350, 400, 450, 500, 550].map {|price| Bento.new price}
puts "【半額品】"
puts セール 弁当リスト, [半額シール]
puts "【おつとめ品】"
puts セール 弁当リスト, [おつとめ品シール, 半額シール]
@Shinpeim
Copy link
Author

Shinpeim commented Feb 4, 2015

  • シールをインスタンス変数に持たせることにした
    sell_price の計算の責務はBentoにあるべきだと思う
  • おつとめ品のロジックを�地の文に合わせた
    地の�文には「いちりつ200円」とあるが、見たところ「一律200円引き」というロジックになっている気がするので地の文に合わせた。地の文をわたしが読み違えているかもしれない。
  • 弁当リストが使いまわされているので、discount メソッドは破壊的な変更はせず、シールを貼られた新しい弁当を返すようにした

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