Class: String

Inherits:
Object show all
Defined in:
lib/boolean.rb

Instance Method Summary collapse

Instance Method Details

#parse_booltrue, false

Returns the parsed value of this object. Strings beginning with any of “y”, “t”, or “1” are considered @true@, whereas all else are considered @false@.

Returns:

  • (true, false)

    The parsed Boolean value of this string.

See Also:



76
# File 'lib/boolean.rb', line 76

def parse_bool() %w( y Y 1 t T ).include? self[0,1] end

#parse_bool!true, false

Similar to #parse_bool, but raises an error unless the string can be explicitly parsed to @true@ or @false@. Strings beginning with “n”, “f”, or “0” are considered false.

Examples:

"true".parse_bool! #=> true
"no".parse_bool! #=> false
"maybe".parse_bool! #=> ArgumentError

Returns:

  • (true, false)

    The parsed Boolean value of this string.

Raises:

  • (ArgumentError)

    If the string does not seem to represent @true@ or @false@.



92
93
94
95
96
97
98
99
100
# File 'lib/boolean.rb', line 92

def parse_bool!
  if %w( y Y 1 t T ).include? self[0,1] then
    true
  elsif %w( n N 0 f F ).include? self[0,1] then
    false
  else
    raise ArgumentError, "Invalid value for parse_bool!: #{inspect}"
  end
end

#to_bObject

See Also:



79
# File 'lib/boolean.rb', line 79

def to_b() parse_bool end

#to_b!Object

See Also:



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

def to_b!() parse_bool! end