Module: Bool

Included in:
FalseClass, TrueClass
Defined in:
lib/safebool.rb

Constant Summary collapse

TRUE_VALUES =
['true', 'yes', 'on', 't', 'y', '1']
FALSE_VALUES =
['false', 'no', 'off', 'f', 'n', '0']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(o) ⇒ Object

used by “global” Bool( o ) kernel conversion method



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/safebool.rb', line 32

def self.convert( o )   ## used by "global" Bool( o ) kernel conversion method

  if o.respond_to?( :parse_bool )
    value = o.parse_bool()  # note: returns true/false OR nil

    if value.nil?
      raise ArgumentError.new( "invalid value >#{o.inspect}< of type >#{o.class.name}< for Bool(); method parse_bool failed (returns nil)")
    end
    value
  else
    raise TypeError.new( "can't convert >#{o.inspect}< of type >#{o.class.name}< to Bool; method parse_bool expected / missing")
  end
end

.parse(o) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/safebool.rb', line 14

def self.parse( o )
  if o.is_a? String
    str = o
  else  ## try "coerce" to string

    str = o.to_str
  end

  case str.downcase.strip
  when *TRUE_VALUES
    true
  when *FALSE_VALUES
    false
  else
    nil   ## note: returns nil if cannot convert to true or false

  end
end

.zeroObject



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

def self.zero() false; end

Instance Method Details

#parse_boolObject



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

def parse_bool() self; end

#to_bObject



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

def to_b()       self; end