Sha256: 50f21937565d07aab67bc5d734e03cf87610326cd73d7480de28bdf3bf79396c

Contents?: true

Size: 796 Bytes

Versions: 14

Compression:

Stored size: 796 Bytes

Contents

class String
  def to_bool
    return true if self == true || self =~ (/^(true|t|yes|y|1)$/i)
    return false if self == false || self.empty? || self =~ (/^(false|f|no|n|0)$/i)
    raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
  end
  #  if using Rails empty? can be changed for blank?
end

if RUBY_VERSION >= "2.4"
  integer_class = Object.const_get("Integer")
else
  integer_class = Object.const_get("Fixnum")
end
integer_class::class_eval do
  def to_bool
    return true if self == 1
    return false if self == 0
    raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
  end
end

class TrueClass
  def to_i; 1; end
  def to_bool; self; end
end

class FalseClass
  def to_i; 0; end
  def to_bool; self; end
end

class NilClass
  def to_bool; false; end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
monza-1.3.8 lib/monza/bool_typecasting.rb
monza-1.3.7 lib/monza/bool_typecasting.rb
monza-1.3.6 lib/monza/bool_typecasting.rb
monza-1.3.5 lib/monza/bool_typecasting.rb
monza-1.3.4 lib/monza/bool_typecasting.rb
monza-1.3.3 lib/monza/bool_typecasting.rb
monza-1.3.2 lib/monza/bool_typecasting.rb
monza-1.3.1 lib/monza/bool_typecasting.rb
monza-1.3.0 lib/monza/bool_typecasting.rb
monza-1.1.3 lib/monza/bool_typecasting.rb
monza-1.1.2 lib/monza/bool_typecasting.rb
monza-1.1.1 lib/monza/bool_typecasting.rb
monza-1.1.0 lib/monza/bool_typecasting.rb
monza-0.1.4 lib/monza/bool_typecasting.rb