Skip to content

Koans AboutTrueAndFalse

Patrick T. Nielsen edited this page Jun 6, 2015 · 1 revision
class AboutTrueAndFalse < Neo::Koan
  def truth_value(condition)
    if condition
      :true_stuff
    else
      :false_stuff
    end
  end

  def test_true_is_treated_as_true
    assert_equal :true_stuff, truth_value(true)
  end

  def test_false_is_treated_as_false
    assert_equal :false_stuff, truth_value(false)
  end

  def test_nil_is_treated_as_false_too
    assert_equal :false_stuff, truth_value(nil)
  end

  def test_everything_else_is_treated_as_true
    assert_equal :true_stuff, truth_value(1)
    assert_equal :true_stuff, truth_value(0)
    assert_equal :true_stuff, truth_value([])
    assert_equal :true_stuff, truth_value({})
    assert_equal :true_stuff, truth_value("Strings")
    assert_equal :true_stuff, truth_value("")
  end

end

Every object in Ruby in Ruby has a Boolean value. Of all of the objects, only two return false and they are the false object and nil object. Everything else returns true.