实际上有4种常用的方法将参数传递给函数。 你的第一个例子是最常见的例子,但是你害怕做出了一个糟糕的例子。您的数量始终是8,所以如果是多余的,也没有使用,因此也多余 的parametere价格这将是相同以下
def order_food price
"8 large Burgers, coming right up!"
end
但是,这是不是你的目的我相信。
所以,这会是这样 第一种方法的东西
def order_food1(size, quantity, price)
extra = quantity == 1 ? :salad : :Burgers
cost = quantity * price
"#{quantity} #{size} #{extra}, coming right up! Thats is #{cost} dollar please"
end
+快(见基准)
+每个人都使用它,了解它在一个糖霜
- 你要知道它们所定义的参数和顺序,你需要阅读的不太常见的应用方法的API
- 你必须提供所有的参数
下一页:使用可选参数使用默认值
def order_food2(size = "large", quantity = 1, price = 4)
extra = quantity == 1 ? :salad : :Burgers
cost = quantity * price
"#{quantity} #{size} #{extra}, coming right up! Thats is #{cost} dollar please"
end
+还广泛应用于
+没有必要,如果他们是在默认的使用参数
- 你还需要知道的参数的顺序和意义,如果最后使用你需要他们所有
下一页:使用哈希作为参数
def order_food3(opts = {})
opts = {size: :large, quantity: 1, price: 4}.merge!(opts)
extra = opts[:quantity] == 1 ? :salad : :Burgers
cost = opts[:quantity] * opts[:price]
"#{opts[:quantity]} #{opts[:size]} #{extra}, coming right up! Thats is #{cost} dollar please"
end
- 少用,你的方法自己是有点难以阅读
- 慢
+不需要知道参数你不不需要,也没有订单
+该方法的使用更具可读性
下一页:以前的方法
def order_food4(size: :large, quantity: 1, price: 4)
extra = :quantity == 1 ? :salad : :Burgers
cost = quantity * price
"#{quantity} #{size} #{extra}, coming right up! Thats is #{cost} dollar please"
end
的简化版本 - 慢
+方法本身和使用它的可读性更强
哪一个更好?取决于个人的品味和情况。 我使用所有的人,并没有什么设计指南,据我知道,prefares一方或另一方。在实践中,你甚至会将其中的一些组合起来。那些很少改变 参数,最好给出一个默认值,反之亦然。 的方法是所谓多次(如递归的)能够从更快,更少的内存消耗方法1. 在我看来受益,可读性是一个Ruby脚本最重要的,所以如果有很多的参数和使用feasable方法3或4
使用和标准的一些例子..
puts order_food1("large", 3, 4)
puts order_food2("large", 3, 4)
puts order_food3(size: "large", quantity: 3, price: 4)
puts order_food3
puts order_food4(size: "large", quantity: 3, price: 4)
puts order_food4
# 3 large Burgers, coming right up! Thats is 12 dollar please
# 3 large Burgers, coming right up! Thats is 12 dollar please
# 3 large Burgers, coming right up! Thats is 12 dollar please
# 1 large salad, coming right up! Thats is 4 dollar please
# 3 large Burgers, coming right up! Thats is 12 dollar please
# 1 large Burgers, coming right up! Thats is 4 dollar please
require 'benchmark'
Benchmark.bmbm do |x|
x.report("order_food1 ") { 10000.times { order_food1("large", 3, 12) }}
x.report("order_food2 ") { 10000.times { order_food2("large", 3, 12) }} # all parameters given
x.report("order_food2_def") { 10000.times { order_food2 }} # using default parameters
x.report("order_food3 ") { 10000.times { order_food3(size: "large", quantity: 3, price: 12) }} # all parameters given
x.report("order_food3 def") { 10000.times { order_food3 }} # using default parameters
x.report("order_food4 ") { 10000.times { order_food3(size: "large", quantity: 3, price: 12) }} # all parameters given
x.report("order_food4 def") { 10000.times { order_food3 }} # using default parameters
end
# user system total real
# order_food1 0.015000 0.000000 0.015000 ( 0.010420)
# order_food2 0.000000 0.000000 0.000000 ( 0.010675)
# order_food2_def 0.016000 0.000000 0.016000 ( 0.011007)
# order_food3 0.015000 0.000000 0.015000 ( 0.020182)
# order_food3 def 0.016000 0.000000 0.016000 ( 0.016954)
# order_food4 0.015000 0.000000 0.015000 ( 0.020256)
# order_food4 def 0.000000 0.000000 0.000000 ( 0.016968)
'size'和'quantity'不是实例变量。这将是'@ size'和'@ quantity'。 – Schwern
完全混合,改变了。由于@Schwern – ekeith
什么是您的两个方法'price'参数的目的是什么? –