Skip to content

Instantly share code, notes, and snippets.

@Keishake
Last active January 3, 2016 05:19
Show Gist options
  • Save Keishake/8414640 to your computer and use it in GitHub Desktop.
Save Keishake/8414640 to your computer and use it in GitHub Desktop.
to_bool method
# put this file at config/initializers
module StringToBoolean
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 String; include StringToBoolean; end
module BooleanToBoolean
def to_bool;return self; end
end
class TrueClass; include BooleanToBoolean; end
class FalseClass; include BooleanToBoolean; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment