Skip to content

Instantly share code, notes, and snippets.

@ryana

ryana/bool_tools.rb

Created Oct 24, 2008
Embed
What would you like to do?
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