Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tinogomes/1435559 to your computer and use it in GitHub Desktop.
Save tinogomes/1435559 to your computer and use it in GitHub Desktop.
symbol.rb
# You can check if current symbol is equivalent the question struing
#
# e.g.:
# :symbol.symbol? # => true
# :symbol1.symbol1? # => true
# :sym_bol.sym_bol? # => true
# :"sym-bol".sym_bol? # => true
# :"sym bol".sym_bol? # => true
class Symbol
def method_missing(meth, *args, &blk)
if meth.to_s =~ /^(\S+)\?$/
$1 == self.to_s.gsub(/(-|\s)/, "_")
else
super(meth, args, &blk)
end
end
end
# To test
if __FILE__ == $0
gender = :male
gender.male? # result: true
gender.felame? # result: false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment