gpt4 book ai didi

ruby - Ruby 中更漂亮的代码

转载 作者:数据小太阳 更新时间:2023-10-29 08:19:07 25 4
gpt4 key购买 nike

这段代码有更漂亮的版本吗?

@available_option_types.delete_if {|ot|
result = true
result = current_user.retailer.id != ot.retailer.id if ot.retailer.present?
result
} unless current_user.has_role? 'admin'

谢谢!

最佳答案

@available_option_types.delete_if { |ot|
ot.retailer.present? ? (current_user.retailer.id != ot.retailer.id) : true
} unless current_user.has_role? 'admin'

或者,如果您将一些逻辑放入模型中,它会更漂亮:

class User
def same_retailer_with?(option_type)
option_type.retailer.present? ? (self.retailer.id != option_type.retailer.id) : true
end
end

@available_option_types.delete_if { |ot| current_user.same_retailer_with?(ot) } unless current_user.has_role? 'admin'

关于ruby - Ruby 中更漂亮的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6586631/

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