Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Last active November 10, 2024 02:11
Show Gist options
  • Save jgaskins/de1f724e980ce29e31ac90eb2b5bab1f to your computer and use it in GitHub Desktop.
Save jgaskins/de1f724e980ce29e31ac90eb2b5bab1f to your computer and use it in GitHub Desktop.
Type coercion for ENV
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