Class: TTY::Coercer::Boolean

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

Overview

A class responsible for boolean type coercion

Class Method Summary collapse

Class Method Details

.coerce(value) ⇒ Object

Coerce value to boolean type including range of strings such as

coerce("False") # => false

other values coerced to false are:
  0, f, F, FALSE, false, False, n, N, No,  no,  No

Examples:

coerce("True") # => true

other values coerced to true are:
  1, t, T, TRUE,  true,  True,  y, Y, YES, yes, Yes

Parameters:

  • value (Object)


25
26
27
28
29
30
31
32
33
34
# File 'lib/tty/coercer/boolean.rb', line 25

def self.coerce(value)
  case value.to_s
  when %r/^(yes|y|t(rue)?|1)$/i
    return true
  when %r/^(no|n|f(alse)?|0)$/i
    return false
  else
    raise TypeError, "Expected boolean type, got #{value}"
  end
end