# boolean.rb # As suggested by Ara T. Howard class String # Interpret common affirmative string meanings as true, # otherwise false. Balnk sapce and case are ignored. # The following strings that will return true: # # true,yes,on,t,1,y,== # # Examples: # # "true".to_b #=> true # "yes".to_b #=> true # "no".to_b #=> false # "123".to_b #=> false # def to_b case self.downcase.strip when 'true', 'yes', 'on', 't', '1', 'y', '==' return true when 'nil', 'null' return nil else return false end end end class Array # Boolean conversion for not empty? def to_b ! self.empty? end end class Numeric # Provides a boolean interpretation of self. # If self == 0 then false else true. # # 0.to_b #=> false # 1.to_b #=> true # 2.3.to_b #=> true # def to_b self == 0 ? false : true end end module Kernel # Boolean conversion for not being nil or false. # Other classes may redefine this to suite the # particular need. # # "abc".to_b #=> true # true.to_b #=> true # false.to_b #=> false # nil.to_b #=> false # def to_b self ? true : false end # Returns true is an object is class TrueClass, # otherwise false. # # true.true? #=> true # false.true? #=> false # nil.true? #=> false # def true? (true == self) end # Returns true is an object is class FalseClass, # otherwise false. # # true.false? #=> false # false.false? #=> true # nil.false? #=> false # def false? (false == self) end # Returns true is an object is class TrueClass # or FalseClass, otherwise false. # # true.bool? #=> true # false.bool? #=> true # nil.bool? #=> false # def bool? (true == self or false == self) end end class Object def to_bool return true end end class TrueClass def to_bool self end end class FalseClass def to_bool self end end class NilClass def to_bool false end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TestStringBoolean < Test::Unit::TestCase def test_to_b assert( 'true'.to_b ) assert( 'True'.to_b ) assert( 'yes'.to_b ) assert( 'YES'.to_b ) assert( 'on'.to_b ) assert( 'ON'.to_b ) assert( 't'.to_b ) assert( '1'.to_b ) assert( 'y'.to_b ) assert( 'Y'.to_b ) assert( '=='.to_b ) assert( ! 'nil'.to_b ) assert( ! 'false'.to_b ) assert( ! 'blahblahtrueblah'.to_b ) assert_equal( nil, 'nil'.to_b ) assert_equal( nil, 'null'.to_b ) end end class TestArrayBoolean < Test::Unit::TestCase def test_to_b a = [] assert_equal( false, a.to_b ) end end class TestNumericBoolean < Test::Unit::TestCase def test_to_b assert_equal( false, 0.to_b ) assert_equal( true, 1.to_b ) end end class TestKernelBoolean < Test::Unit::TestCase def test_to_b assert_equal( true, true.to_b ) assert_equal( false, false.to_b ) assert_equal( false, nil.to_b ) end def test_false? assert( false.false? ) assert( (1 == 2).false? ) assert( ! (1 == 1).false? ) end def test_true? assert( true.true? ) assert( (1 == 1).true? ) assert( ! (1 == 2).true? ) end def test_bool? assert_equal( true, true.bool? ) assert_equal( true, false.bool? ) assert_equal( false, nil.bool? ) assert_equal( false, 0.bool? ) end end class TestBoolean < Test::Unit::TestCase def test_to_bool assert_equal( true, true.to_bool ) assert_equal( false, false.to_bool ) assert_equal( false, nil.to_bool ) end end =end