Module: Jinx::Boolean

Included in:
FalseClass, TrueClass
Defined in:
lib/jinx/helpers/boolean.rb

Overview

Boolean marks the true and false primitive objects.

Constant Summary collapse

TRUE_REGEXP =

The match for a true String.

/^(t(rue)?|y(es)?|1)$/i
FALSE_REGEXP =

The match for a false String.

/^(f(alse)?||no?|0)$/i

Class Method Summary collapse

Class Method Details

.for(obj) ⇒ Boolean?

Converts the given object to a Boolean as follows:

  • If the object is nil or Boolean, then the unconverted value

  • 1 -> true

  • 0 -> false

  • A TRUE_REGEXP match -> true

  • A FALSE_REGEXP match -> false

Parameters:

  • for

    the object to convert

Returns:

  • (Boolean, nil)

    the corresponding Boolean

Raises:

  • (ArgumentError)

    if the value cannot be converted



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jinx/helpers/boolean.rb', line 20

def self.for(obj)
  case obj
  when nil then nil
  when true then true
  when false then false
  when 1 then true
  when 0 then false
  when TRUE_REGEXP then true
  when FALSE_REGEXP then false
  else
    raise ArgumentError.new("Value cannot be converted to boolean: '#{obj}'")
  end
end