我正在研究这段代码,以使其更短,以便于学习或阅读,并且我已经应用干练和扎实的原理,所以您能帮助我如何重构此ruby on rails代码吗?包含一些if语句来执行此操作。因此使用这些原理,它变得更易于阅读和缩短。
if brand.rating = high
if price_reference < 15
price_suggestion = (price_reference - product_database.shipping_cost * 0.75) * 1.1
price_original = price_suggestion
elsif price_reference < 25
price_suggestion = (price_reference - product_database.shipping_cost * 0.775) * 1.1
price_original = price_suggestion
elsif price_reference < 50
price_suggestion = (price_reference - product_database.shipping_cost * 0.8) * 1.1
price_original = price_suggestion
elsif price_reference < 75
price_suggestion = (price_reference - product_database.shipping_cost * 0.825) * 1.1
price_original = price_suggestion
elsif price_reference < 100
price_suggestion = (price_reference - product_database.shipping_cost * 0.85) * 1.1
price_original = price_suggestion
elsif price_reference < 200
price_suggestion = (price_reference - product_database.shipping_cost * 0.875) * 1.1
price_original = price_suggestion
elsif price_reference < 400
price_suggestion = (price_reference - product_database.shipping_cost * 0.9) * 1.1
price_original = price_suggestion
elsif price_reference < 600
price_suggestion = (price_reference - product_database.shipping_cost * 0.925) * 1.1
price_original = price_suggestion
else
price_suggestion = (price_reference - product_database.shipping_cost * 0.95) * 1.1
price_original = price_suggestion
elsif brand.rating = low
if price_reference < 15
price_suggestion = (price_reference - product_database.shipping_cost * 0.75) * 0.9
price_original = price_suggestion
elsif price_reference < 25
price_suggestion = (price_reference - product_database.shipping_cost * 0.775) * 0.9
price_original = price_suggestion
elsif price_reference < 50
price_suggestion = (price_reference - product_database.shipping_cost * 0.8) * 0.9
price_original = price_suggestion
elsif price_reference < 75
price_suggestion = (price_reference - product_database.shipping_cost * 0.825) * 0.9
price_original = price_suggestion
elsif price_reference < 100
price_suggestion = (price_reference - product_database.shipping_cost * 0.85) * 0.9
price_original = price_suggestion
elsif price_reference < 200
price_suggestion = (price_reference - product_database.shipping_cost * 0.875) * 0.9
price_original = price_suggestion
elsif price_reference < 400
price_suggestion = (price_reference - product_database.shipping_cost * 0.9) * 0.9
price_original = price_suggestion
elsif price_reference < 600
price_suggestion = (price_reference - product_database.shipping_cost * 0.925) * 0.9
price_original = price_suggestion
else
price_suggestion = (price_reference - product_database.shipping_cost * 0.95) * 0.9
price_original = price_suggestion
else
if price_reference < 15
price_suggestion = (price_reference - product_database.shipping_cost * 0.75) * 1
price_original = price_suggestion
elsif price_reference < 25
price_suggestion = (price_reference - product_database.shipping_cost * 0.775) * 1
price_original = price_suggestion
elsif price_reference < 50
price_suggestion = (price_reference - product_database.shipping_cost * 0.8) * 1
price_original = price_suggestion
elsif price_reference < 75
price_suggestion = (price_reference - product_database.shipping_cost * 0.825) * 1
price_original = price_suggestion
elsif price_reference < 100
price_suggestion = (price_reference - product_database.shipping_cost * 0.85) * 1
price_original = price_suggestion
elsif price_reference < 200
price_suggestion = (price_reference - product_database.shipping_cost * 0.875) * 1
price_original = price_suggestion
elsif price_reference < 400
price_suggestion = (price_reference - product_database.shipping_cost * 0.9) * 1
price_original = price_suggestion
elsif price_reference < 600
price_suggestion = (price_reference - product_database.shipping_cost * 0.925) * 1
price_original = price_suggestion
else
price_suggestion = (price_reference - product_database.shipping_cost * 0.95) * 1
price_original = price_suggestion
end
答案 0 :(得分:3)
也许像这样:
def suggest_price(price_reference, shipping_cost)
[
[15, 0.75],
[25, 0.775],
[50, 0.8],
... etc ...
].each do |price_point, shipping_modifier|
if price_reference < price_point
return price_reference - shipping_cost * shipping_modifier
end
end
return price_reference - shipping_cost * 0.95
end
def price_modifier(brand)
if brand.rating == high
1.1
elsif brand.rating == low
0.9
else
1
end
end
price_original = price_modifier(brand) * suggest_price(price_reference, product_database.shipping_cost)
我并不是说那是完美的代码,但这是您可以使用的一种方式。您可能想要一个恒定的价格点和修饰符。
答案 1 :(得分:1)
您可以应用一些OOP。在Brand
类中,您可以使用一些.case
语句使其更具可读性...
def prices
case self.rating
when 'high"
case
when price_reference < 15
(price_reference - product_database.shipping_cost * 0.75) * 1.1
when price_reference < 25
(price_reference - product_database.shipping_cost * 0.775) * 1.1
when price_reference < 50
(price_reference - product_database.shipping_cost * 0.8) * 1.1
etc...
when 'low'
case
etc...
else
etc...
end
end
嵌套的case
语句更易于阅读。另外,您正在执行许多不需要的变量重新分配。请记住,Ruby总是返回它评估的最后一条语句。因此,当您在Brand类的成员上调用方法时,可以像这样使用它:
brand = Brand.new(rating: 'high', price_reference: '25')
#let's assume product_database.shipping_cost = 5
brand.prices
#returns the value 23.2375 which came from (25 - 5 * 0.775) * 1.1
有很多方法可以做到这一点。通过创建一些哈希字典来包含每个公式的值,然后进行变量替换来调用它们,您可以抽象出更多代码。尝试根据对象和方法进行操作。 Ruby中的所有内容都是某种对象,您可以创建方法来调用那些调用其他方法并使用其他对象的对象。