Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在一行中编写条件语句?轨道_Ruby On Rails_Ruby_Refactoring - Fatal编程技术网

Ruby on rails 如何在一行中编写条件语句?轨道

Ruby on rails 如何在一行中编写条件语句?轨道,ruby-on-rails,ruby,refactoring,Ruby On Rails,Ruby,Refactoring,我想说的是 self.preferred_amount * object.each{|li|li.variant}.collect{|li|li.weight} 唯一的问题是某些权重等于零 在这种情况下,我想补充一点,如果它们等于零,就等于0 有没有办法将这种逻辑整合到同一行中 或者有什么方法可以使这个语句比它更具重构性?将li.weight更改为li.weight | | 0 ||是短路还是操作故障。如果它的左侧为truthy,既不为false也不为nil,则返回左侧,否则返回右侧 MRI>

我想说的是

self.preferred_amount * object.each{|li|li.variant}.collect{|li|li.weight}
唯一的问题是某些权重等于零

在这种情况下,我想补充一点,如果它们等于零,就等于0

有没有办法将这种逻辑整合到同一行中

或者有什么方法可以使这个语句比它更具重构性?

将li.weight更改为li.weight | | 0

||是短路还是操作故障。如果它的左侧为truthy,既不为false也不为nil,则返回左侧,否则返回右侧

MRI>=1.8.7中有一个功能可以让您更简洁。而不是:

each{|li|li.variant}
你可以写

each(&:variant)
在Ruby 1.8.7之前的版本中,需要backports gem才能获得此功能

比这更好的是,将所有逻辑移到对象的类中,例如

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

self.preferred_amount * object.variant_weights
self.preferred_amount * object.total_variant_weights
但是,请注意,将标量量乘以数组是一个错误。如果您打算对权重求和,则:

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

self.preferred_amount * object.variant_weights
self.preferred_amount * object.total_variant_weights
将li.weight更改为li.weight | 0

||是短路还是操作故障。如果它的左侧为truthy,既不为false也不为nil,则返回左侧,否则返回右侧

MRI>=1.8.7中有一个功能可以让您更简洁。而不是:

each{|li|li.variant}
你可以写

each(&:variant)
在Ruby 1.8.7之前的版本中,需要backports gem才能获得此功能

比这更好的是,将所有逻辑移到对象的类中,例如

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

self.preferred_amount * object.variant_weights
self.preferred_amount * object.total_variant_weights
但是,请注意,将标量量乘以数组是一个错误。如果您打算对权重求和,则:

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

self.preferred_amount * object.variant_weights
self.preferred_amount * object.total_variant_weights
只需| | 0重量:

self.preferred_amount * object.each{|li|li.variant}.collect{|li|li.weight || 0}
只需| | 0重量:

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

试一试


请注意,以上所有答案都是正确的,但为了直接回答您的问题:

如何在单行中编写条件语句?轨道

可以使用三元运算符。它们的形式如下:

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

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' %>
就像我说的,上面的答案实际上是这个特定操作的答案,而不是三元运算符在这种情况下不起作用。我只是想让你更深入地了解一句台词


还要注意,这不是特定于Ruby的。大多数编程语言包括Ruby、PHP、CF、AS、JAVA、C、C。。。请使用三元运算符。

注意,以上所有答案都是正确的,但要直接回答您的问题:

如何在单行中编写条件语句?轨道

可以使用三元运算符。它们的形式如下:

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

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' %>
就像我说的,上面的答案实际上是这个特定操作的答案,而不是三元运算符在这种情况下不起作用。我只是想让你更深入地了解一句台词


还要注意,这不是特定于Ruby的。大多数编程语言包括Ruby、PHP、CF、AS、JAVA、C、C。。。有三元运算符。

每个运算符似乎都是多余的。那么:

self.preferred_amount * object.collect { |o| o.variant.weight.to_i }
或者,如果你真的想对权重求和:


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

每一个似乎都是多余的。那么:

self.preferred_amount * object.collect { |o| o.variant.weight.to_i }
或者,如果你真的想对权重求和:


self.preferred|amount*object.inject{| sum,o | sum+o.variant.weight.to|i}

你能解释一下object.each在做什么吗?用数组乘以标量是个错误。你的意思是计算重量的总和吗?@wayne,是的,但我后来加了这个。非常感谢你能解释一下这个对象在做什么吗?用一个数组乘以一个标量是一个错误。你的意思是计算重量的总和吗?@wayne,是的,但我后来加了这个。谢谢这么好的回答,我有这样的@models=Model.find:all.collect{m | modelist[m.id]>0?m:false},像这样我被迫提供一个替代值,否则我会得到一个错误,有没有办法让它在左边有一个值或者什么都不做好的答案,我有这样的@models=Model.find:all.collect{| m | modellist[m.id]>0?m:false},像这样,我被迫提供一个替代值,否则我会得到一个错误,有没有办法让它在左边有一个值或者什么都不做