Skip to content

Instantly share code, notes, and snippets.

  • Save lucascaton/1447386 to your computer and use it in GitHub Desktop.
Save lucascaton/1447386 to your computer and use it in GitHub Desktop.
symbol.rb
# You can check if current symbol is equivalent the question string
#
# 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