Created
March 9, 2016 20:08
-
-
Save korkey128k/50307cb6c36b9d752827 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def to_bool | |
return true if self == true || self =~ (/^(true|t|yes|y|1|1\.0)$/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 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 | |
class BigDecimal | |
def to_bool; !zero?; end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment