简体   繁体   English

只要X为真,则X,否则为Y

[英]Whenever X is true do X, otherwise Y

Think I'm brain dead. 想我死了。 I need a really concise way of writing when X exists (the value for a specified key in a hash), print that value (ie, X), otherwise, print 0. The reason it needs to be concise is because I'm using it in a options for a form_tag , and I'd like to avoid writing a method. 当X存在时(哈希中指定键的值),我需要一种非常简洁的书写方式,打印该值(即X),否则打印0。需要简洁的原因是因为我正在使用它在form_tagoptions中,我希望避免编写方法。

Can't think this through... here's what I've tried: 不能这样想...这是我尝试过的方法:

  1. This won't work because it will always return "0": <%= number_field_tag "transaction[][#{thing}]", :quantity, min: 0, placeholder: 0 || @transactionparams["#{thing}"] %> 这将行不通,因为它将始终返回“ 0”: <%= number_field_tag "transaction[][#{thing}]", :quantity, min: 0, placeholder: 0 || @transactionparams["#{thing}"] %> <%= number_field_tag "transaction[][#{thing}]", :quantity, min: 0, placeholder: 0 || @transactionparams["#{thing}"] %>

  2. This won't work because whenever @transactionparams doesn't exist, instead of getting 0, I get an error undefined method []' for nil:NilClass <%= number_field_tag "transaction[][#{thing}]", :quantity, min: 0, placeholder: @transactionparams["#{thing}"] %>` 这是行不通的,因为每当@transactionparams不存在时,而不是得到0时,我得到一个错误的未定义方法[]' for nil:NilClass <%= number_field_tag“ transaction [] [#{thing}]”“ []' for nil:NilClass quantity ,min:0,占位符:@transactionparams [“#{thing}”]%>`

If you're using Ruby 2+, you can take advantage of the fact that nil.to_h is an empty Hash: 如果您使用的是Ruby 2+,则可以利用nil.to_h为空哈希的事实:

v = @transactionparams.to_h[:thing] || 0

You can also take advantage of nil.to_i being zero: 您还可以利用nil.to_i为零的优势:

v = @transactionparams.to_h[:thing].to_i

Note that Rails will patch NilClass#to_h to return an empty Hash too so this will work even if you're not in Ruby 2+. 请注意,Rails也会修补NilClass#to_h来返回一个空的Hash,因此即使您不在Ruby 2+中也可以使用它。

These assume that you're not expecting nil to be a value in the Hash. 这些假定您不期望nil是哈希中的值。 If you want to allow nil values through then use fetch with a second argument: 如果要允许通过nil值,则将fetch与第二个参数一起使用:

v = @transactionparams.to_h.fetch(:thing, 0)

Since you're in Rails, you could throw a try into the mix to swallow up the nil values of the instance variable: 由于您在Rails中,因此可以try混合以吞下实例变量的nil值:

v = @transactionparams.try(:fetch, :thing, 0).to_i

The trailing to_i call is there because nil.try(m) is nil for all m . 尾随的to_i调用在那里,因为所有m nil.try(m)均为nil You could also call #[] explicitly and avoid the second "return this instead of raising an exception" argument to Hash#fetch : 您还可以显式调用#[]并避免对Hash#fetch使用第二个“返回此值而不引发异常”参数:

v = @transactionparams.try(:[], :thing).to_i

You can mix and match the above depending on what sorts of values you expect to have in @transactionparams and how specifically you need to handle nil s. 您可以根据期望在@transactionparams使用哪种类型的值以及需要特殊处理nil的方式来混合和匹配以上内容。 I'd probably go with to_h and to_i in most cases: 在大多数情况下,我可能会使用to_hto_i

@transactionparams.to_h[:thing].to_i

I tend to use .to_X methods ( #to_a in particular) to hide nil checks all the time so I would immediately recognize the purpose of to_h and to_i in that expression. 我倾向于使用.to_X方法(尤其是#to_a )来一直隐藏nil检查,因此我会立即认识到该表达式中to_hto_i的目的。

How about a ternary operator? 三元运算符呢?

@transactionparams = {:thing => "foo"}
puts @transactionparams ? @transactionparams[:thing] : 0
@transactionparams = nil 
puts @transactionparams ? @transactionparams[:thing] : 0

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

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