Module: Kernel

Defined in:
lib/safebool.rb

Instance Method Summary collapse

Instance Method Details

#Bool(o) ⇒ Object

“global” conversion function / method



131
# File 'lib/safebool.rb', line 131

def Bool( o ) Bool.convert( o ); end

#bool?Boolean

Returns true if object class is TrueClass or FalseClass, otherwise false.

true.bool? #=> true false.bool? #=> true nil.bool? #=> false

Returns:

  • (Boolean)


87
# File 'lib/safebool.rb', line 87

def bool?() self.class == TrueClass || self.class == FalseClass; end

#false?Boolean

Returns true if object class is FalseClass, otherwise false.

false.false? #=> true true.false? #=> false nil.false? #=> false

Returns:

  • (Boolean)


95
# File 'lib/safebool.rb', line 95

def false?() self.class == FalseClass; end

#to_bObject

default “explicit” conversion to bool for all objects



108
109
110
111
112
113
114
115
116
# File 'lib/safebool.rb', line 108

def to_b
  if respond_to?( :parse_bool )
    value = parse_bool()         # note: returns true/false/nil  (nil for error/cannot parse)
    value = true  if value.nil?  #         default nil (cannot parse to bool) to true
    value
  else
    self ? true : false    ## use "standard" ruby semantics, that is, everything is true except false & nil
  end
end

#to_boolObject

to_bool - “porcelain” method “alias” for parse_bool; use parse_bool for “internal” use and to_bool for “external” use note: by “default” the method parse_bool is undefined (!);

define parse_bool in concrete / derived class to add bool conversion support


122
123
124
125
126
127
128
# File 'lib/safebool.rb', line 122

def to_bool()
  if respond_to?( :parse_bool )
    parse_bool()
  else
    nil   ## note: returns nil if can't convert to true or false
  end
end

#true?Boolean

Returns true if object class is TrueClass, otherwise false.

true.true? #=> true false.true? #=> false nil.true? #=> false

Returns:

  • (Boolean)


103
# File 'lib/safebool.rb', line 103

def true?() self.class == TrueClass; end