Ruby: True & False

,

true and false are builtin objects.

The following evaluates to false:

Everything else is true (including 0, 0.0, "0", "", []). (this is similar to emacs lisp. 〔➤ Emacs Lisp Basics〕)

# -*- coding: utf-8 -*-
# ruby

if false then p "y" else p "n" end # "n"

if nil then p "y" else p "n" end   # "n"

if () then p "y" else p "n" end # "n". Empty paren eval to nil, so false.
# -*- coding: utf-8 -*-
# ruby

if true then p "y" else p "n" end # "y"

if 0 then p "y" else p "n" end    # "y"

if 0.0 then p "y" else p "n" end  # "y"

if [] then p "y" else p "n" end   # "y"

if {} then p "y" else p "n" end   # "y"

if "" then p "y" else p "n" end   # "y"
blog comments powered by Disqus