Skip to content

Instantly share code, notes, and snippets.

@steveh
Created November 13, 2018 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveh/d3e9cc736c5e5f2d49f7d417a57bf402 to your computer and use it in GitHub Desktop.
Save steveh/d3e9cc736c5e5f2d49f7d417a57bf402 to your computer and use it in GitHub Desktop.
class StringBoolean
class << self
TRUE = "1".freeze
FALSE = "0".freeze
TRUTHY = ["1", "t", "y", "true", "yes"].freeze
FALSEY = ["0", "f", "n", "false", "no", ""].freeze
def true
TRUE
end
def false
FALSE
end
def truthy?(value)
TRUTHY.include?(value.to_s.downcase)
end
def falsey?(value)
FALSEY.include?(value.to_s.downcase)
end
end
end
@barnaclebarnes
Copy link

Added another method truthy_or_falsey? to check that the value is either true or false so will return false if you send an invalid string

def truthy_or_falsey?(value)
  FALSEY.include?(value.to_s.downcase) || TRUTHY.include?(value.to_s.downcase)
end

# False
# StringBoolean.truthy_or_falsey?("Bob")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment