ruby lambda_pricing.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby lambda_pricing.rb相关的知识,希望对你有一定的参考价值。

food_prices_before_tax = [9, 8, 7, 6, 5, 4, 3, 2, 1]
liquor_prices_before_tax = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
wine_prices_before_tax = [50, 40, 30, 20, 10]

food_tax = lambda { |price| price + (price * 0.07) }
liquor_tax = lambda { |price| price + (price * 0.17) }

tuesday_wine_discount = lambda { |price| price - (price * 0.15) }

food_prices_with_tax = food_prices_before_tax.map(&food_tax)
liquor_prices_with_tax = liquor_prices_before_tax.map(&liquor_tax)
wine_prices_with_tuesday_discount = wine_prices_before_tax.map(&tuesday_wine_discount)
wine_prices_with_tuesday_discount_and_tax = wine_prices_with_tuesday_discount.map(&liquor_tax)

p food_prices_with_tax
p liquor_prices_with_tax
p wine_prices_with_tuesday_discount
p wine_prices_with_tuesday_discount_and_tax

one_bottle_of_wine = 70
p liquor_tax.call(one_bottle_of_wine)
p liquor_tax.(one_bottle_of_wine)
p liquor_tax[one_bottle_of_wine]

costco_discount = ->(price) { price - (price * 0.05) }
car_prices = [70_000, 60_000, 50_000, 40_000]
car_prices.each do |car|
  puts "With the Costco discount, the car costs: #{costco_discount.call(car)}"
end

以上是关于ruby lambda_pricing.rb的主要内容,如果未能解决你的问题,请参考以下文章

Ruby运算符

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby

什么是ruby?

ruby和ruby ee