What evaluates to false in Ruby?

前端 未结 4 1532
逝去的感伤
逝去的感伤 2020-11-30 13:37

false and nil evaluate to false in Ruby. Anything else? Please provide links to official/authoritative references.

相关标签:
4条回答
  • 2020-11-30 14:07

    You just found them all

    In Ruby, false and nil are “falsy”, while all other values are “truthy”

    as Yehuda Katz mentioned in his blog post in 2009

    0 讨论(0)
  • 2020-11-30 14:10

    false and nil are the only ones:

    http://www.ruby-doc.org/core-2.1.1/FalseClass.html

    Rails provides present? which also includes empty strings and empty arrays: http://api.rubyonrails.org/classes/Object.html#method-i-present-3F

    0 讨论(0)
  • 2020-11-30 14:31

    I can only agree, that nil and false are indeed the only two values, that evaluate to false in Ruby. I just wanted to point to another useful resource:

    True and False vs. "Truthy" and "Falsey" (or "Falsy") in Ruby, Python, and JavaScript

    0 讨论(0)
  • 2020-11-30 14:33

    The only false-y values in Ruby are false and nil, as attested to by the book "The Ruby Programming Language", by David Flanagan and Yukihiro Matsumoto (the Matz):

    Predicates typically return one of the Boolean values true or false, but this is not required, as any value other than false or nil works like true when a Boolean value is required. (The Numeric method nonzero?, for example, returns nil if the number it is invoked on is zero, and just returns the number otherwise.)

    (emphasis mine)

    This quote can be found on page 180 of the book.

    0 讨论(0)
提交回复
热议问题