Skip to content

Instantly share code, notes, and snippets.

@FrendaWinter
Last active November 6, 2023 09:41
Show Gist options
  • Save FrendaWinter/902f819cf987d57394f7ed1ee56b69a8 to your computer and use it in GitHub Desktop.
Save FrendaWinter/902f819cf987d57394f7ed1ee56b69a8 to your computer and use it in GitHub Desktop.
String to boolean with Ruby
#!/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