在Ruby类中实现真实性 [英] Implement truthiness in Ruby class
本文介绍了在Ruby类中实现真实性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Ruby类是否有可能实现一个真实性方法,或者是否所有假对象和nil对象在设计时都会自动为真?
Is it possible for a Ruby class to implement a truthiness method or are all objects beside false and nil automatically true by design?
推荐答案
第二个问题的答案是:是的,在Ruby中,除false
和nil
之外的所有内容都被认为是真实的".
The answer to your second question is: yes, everything else than false
and nil
is considered "truthy" in Ruby.
例如,
a = 'foo' if 0
# => "foo"
a = 'foo' if []
# => "foo"
a = 'foo' if ''
# => "foo"
a = 'foo' if nil
# => nil
a = 'foo' if false
# => nil
这篇关于在Ruby类中实现真实性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文