Created
December 5, 2011 21:53
-
-
Save tinogomes/1435559 to your computer and use it in GitHub Desktop.
symbol.rb
This file contains hidden or 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
# 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