简体   繁体   English

如何在一行中编写条件语句? 栏杆

[英]How do write conditional statement in a single line? rails

I am trying to say this 我想说这个

self.preferred_amount * object.each{|li|li.variant}.collect{|li|li.weight}

The only problem is that certain weights equal nil. 唯一的问题是某些权重等于零。

Being that the case, I would like to add that if they do equal nil, make them equal 0. 既然如此,我想补充一点,如果它们等于nil,则使它们等于0。

Is there any way to incorporate this logic in the same line? 有什么办法可以将这种逻辑整合到同一行中吗?

Or is there a way I can make this statement even more refactored than it is? 还是有一种方法可以使此语句的重构程度更高?

Change li.weight to li.weight || 0 更改li.weightli.weight || 0 li.weight || 0

|| is the "short circuit or" operator. 是“短路或”运算符。 If its left hand side is truthy (neither false nor nil), it returns the left hand side, otherwise it returns the right hand side. 如果它的左手边是真实的(既不是false也不是nil),则返回左手边,否则返回右手边。

There is a feature in MRI >= 1.8.7 that will let you make this terser. MRI> = 1.8.7中有一项功能,可让您制作此地形。 Instead of: 代替:

each{|li|li.variant}

you can write 你可以写

each(&:variant)

In versions of Ruby before 1.8.7, require the backports gem to get this feature. 在1.8.7之前的Ruby版本中,需要使用backports gem来获得此功能。

Better than that, move all of the logic into object's class, eg 更好的是,将所有逻辑移到对象的类中,例如

class Whatever
  def variant_weights
    each(&:variant).collect{ |li| li.weight || 0}
  end
end

and to use it: 并使用它:

self.preferred_amount * object.variant_weights

However, note that it is a bug to multiply a scalar amount by an array. 但是,请注意,将标量乘以数组是一个错误。 If you mean to sum the weights, then: 如果您打算对权重求和,则:

class Whatever
  def total_variant_weights
    each(&:variant).collect{ |li| li.weight || 0}.inject(&:+)
  end
end

and to use it: 并使用它:

self.preferred_amount * object.total_variant_weights

Note, all the answers above are correct for your purpose, but to answer your question directly: 请注意,以上所有答案对于您的目的都是正确的,但可以直接回答您的问题:

How do I write a conditional statement in a single line? 如何单行编写条件语句? Rails 滑轨

You can use ternary operators. 您可以使用三元运算符。 They take the following form: 它们采用以下形式:

assertion ? value_if_true : value_if_false
# if assertion is true, then value_if_true, otherwise, value_if_false

for example: 例如:

puts 4 < 5 ? 'you are on Earth' : 'you are on another planet'
<%= @user.is_admin? ? 'you can access this page' : 'you aren\'t allowed to be here' %>

Like I said, the answers above are actually what you want for this particular operation (not that a ternary operator won't work in this case). 就像我说的那样,上面的答案实际上是您要针对此特定操作执行的操作(不是在这种情况下,三元运算符不起作用)。 I just wanted to give you some more insight into one-liners. 我只是想给您一些有关单线的见解。

Also note, this is not Ruby-specific. 另请注意,这不是特定于Ruby的。 Most programming languages (including Ruby, PHP, CF, AS, JAVA, C, C#...) have ternary operators. 大多数编程语言(包括Ruby,PHP,CF,AS,JAVA,C,C#...)都具有三元运算符。

just || 0 只是|| 0 || 0 the weight: || 0重量:

self.preferred_amount * object.each{|li|li.variant}.collect{|li|li.weight || 0}

尝试

 .collect{|li|li.weight || 0}

The each seems redundant. 每个似乎都是多余的。 What about: 关于什么:

self.preferred_amount * object.collect { |o| o.variant.weight.to_i }

or if you really meant to sum the weights: 或者如果您真的想对权重求和:

self.preferred_amount * object.inject { |sum, o| sum + o.variant.weight.to_i }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM