Sha256: 3a0890ca14a807ad3761cd35325e338c690fda67eda5496d6deb4b245966cf9c

Contents?: true

Size: 655 Bytes

Versions: 2

Compression:

Stored size: 655 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

class Fixnum
  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

2 entries across 2 versions & 1 rubygems

Version Path
monza-0.1.3 lib/monza/bool_typecasting.rb
monza-0.1.2 lib/monza/bool_typecasting.rb