【发布时间】:2018-11-20 03:31:17
【问题描述】:
我有三个数值变量:on_the_ground、double_round_trip 和 point_to_point。 price 函数根据一些简单的逻辑返回其中之一。
下面是它目前的工作方式。
def price
return on_the_ground if date_range == 1
values = [
on_the_ground,
double_round_trip
]
if !turbo? && !vlj?
values.push(point_to_point)
end
values.compact.min
end
我想要一个函数,它可以根据应该返回的值返回一个符号。例如:
def price_name
return :on_the_ground if date_range == 1
... etc...
end
【问题讨论】:
-
一个方法可以返回一个
Symbol,就像上面的price_name方法一样。究竟是什么问题? -
我需要得到
values.compact.min选择的变量名。 -
为此,您需要值和名称之间的映射。使用
Hash来做到这一点。