Created
October 24, 2016 20:21
-
-
Save tobiashm/40c09485e76ce0a9f416fbaaa5dbee2b to your computer and use it in GitHub Desktop.
Boolean conversion method in Ruby
This file contains hidden or 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
module Kernel | |
def Boolean(input) | |
case input | |
when true, /^(true|y|yes|1)$/i, 1 | |
true | |
when false, /^(false|n|no|0)$/i, 0, "", nil | |
false | |
else | |
fail TypeError, "can't convert #{input.inspect} into boolean" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment