Last active
November 6, 2023 09:41
-
-
Save FrendaWinter/902f819cf987d57394f7ed1ee56b69a8 to your computer and use it in GitHub Desktop.
String to boolean with Ruby
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
#!/usr/bin/env ruby | |
def to_b(string) | |
case string | |
when /^(true|t|yes|y|1)$/i then true | |
when /^(false|f|no|n|0)$/i then false | |
else raise "Cannot convert to boolean: #{string}" | |
end | |
end | |
puts to_b("true") | |
puts to_b("false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment