Sha256: 662944894233cda9df140ab58808c4fc2fccc37b8e36310d53c9bf70f89253ad

Contents?: true

Size: 771 Bytes

Versions: 1

Compression:

Stored size: 771 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.blank? || self =~ (/^(false|f|no|n|0)$/i)
    raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
  end
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 Float
  def to_bool
    return true if self == 1.0
    return false if self == 0.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

1 entries across 1 versions & 1 rubygems

Version Path
gemgento-2.8.0 config/initializers/boolean_typecasting.rb