gpt4 book ai didi

ruby-on-rails - 有没有办法涂干而结实的本金

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:07 27 4
gpt4 key购买 nike

我正在研究这段代码以使其更短,以便更容易学习或阅读,而且我已经进一步应用了干练和扎实的原则,所以你能帮我重构这个 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

最佳答案

也许是这样的:

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)

我并不是说那是完美的代码,但这是您可以使用它的一种方式。您可能希望价格点和修饰符保持不变。

关于ruby-on-rails - 有没有办法涂干而结实的本金,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51047857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com