Last active
November 10, 2024 02:11
-
-
Save jgaskins/de1f724e980ce29e31ac90eb2b5bab1f to your computer and use it in GitHub Desktop.
Type coercion for ENV
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 << ENV | |
def integer(key) | |
if value = self[key] | |
value.to_i | |
end | |
end | |
def boolean(key) | |
case value = self[key] | |
when /true/i | |
true | |
when nil, "", /false/i | |
false | |
when /\A\d+\z/ | |
value != "0" | |
else | |
raise ArgumentError.new("Could not coerce #{value.inspect} (via ENV[#{key.inspect}]) to a boolean") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment