Skip to content

Instantly share code, notes, and snippets.

@ryana
Created October 24, 2008 00:25
Show Gist options
  • Save ryana/19291 to your computer and use it in GitHub Desktop.
Save ryana/19291 to your computer and use it in GitHub Desktop.
annoys me to no end that to_bool isnt built in to ruby and that to_i isnt built into boolean classes. fixed.
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 Fixnum
def to_bool
self == 0 ? false : true
end
end
class String
def to_bool
self == "0" ? false : true
end
end
class NilClass
def to_bool
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment