Ruby: True, False

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])

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

if false then p "yes" else p "no" end # "no"

if nil then p "yes" else p "no" end # "no"

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

if true then p "yes" else p "no" end # "yes"

if 0 then p "yes" else p "no" end # "yes"

if 0.0 then p "yes" else p "no" end # "yes"

if [] then p "yes" else p "no" end # "yes"

if {} then p "yes" else p "no" end # "yes"

if "" then p "yes" else p "no" end # "yes"

If you have a question, put $5 at patreon and message me.

  1. Ruby Basics
  2. Doc Lookup
  3. Print Version String
  4. What's RVM, Gem, Rake, Bundler, RDoc, ri, irb?

  1. Unicode 💎
  2. Quote String
  3. Quote Long String
  4. Format String
  5. String Operations
  6. True, False
  7. “if then else”
  8. for, while, Loop
  9. List
  10. Loop Thru List
  11. Hash Table
  12. Function Optional Parameter
  13. Map f to List
  14. Complex Numbers
  1. Intro to Reading Ruby Doc: What's M, C, ::, # ?
  2. Predefined Global Variables