Ruby: True/False (boolean)

By Xah Lee. Date: . Last updated: .

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. [see Emacs Lisp Basics])

# ruby

p((if false then 3 else 4 end) == 4)

p((if nil then 3 else 4 end) == 4)

p((if () then 3 else 4 end) == 4)
# Empty paren eval to nil, so false.
# ruby

p(if true then 3 else 4 end == 3)
p(if 0 then 3 else 4 end == 3)
p(if 0.0 then 3 else 4 end == 3)
p(if [] then 3 else 4 end == 3)
p(if {} then 3 else 4 end == 3)
p(if "" then 3 else 4 end == 3)

# ruby

p true.methods

[:===, :^, :|, :inspect, :&, :to_s, :singleton_class, :dup, :itself,
:taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :methods,
:singleton_methods, :protected_methods, :private_methods, :public_methods,
:instance_variables, :instance_variable_get, :instance_variable_set,
:instance_variable_defined?, :remove_instance_variable, :instance_of?,
:kind_of?, :is_a?, :display, :hash, :class, :frozen?, :tap, :then,
:yield_self, :public_send, :extend, :clone, :method, :public_method,
:singleton_method, :define_singleton_method, :<=>, :=~, :!~, :nil?,
:eql?, :respond_to?, :freeze, :object_id, :send, :to_enum, :enum_for,
:__send__, :!, :instance_eval, :==, :instance_exec, :!=, :equal?, :__id__]