Created
May 4, 2010 05:03
-
-
Save indirect/388981 to your computer and use it in GitHub Desktop.
ruby hashes like JS objects
This file contains 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
class Hash | |
def method_missing( msg, *args ) | |
super unless args.empty? | |
if has_key?(msg) | |
[msg] | |
elsif has_key?(msg.to_s) | |
[msg.to_s] | |
else | |
nil | |
end | |
end | |
end | |
hash = { | |
:cats => "awesome", | |
"dogs" => "okay I guess?" | |
} | |
puts hash.cats | |
puts hash.dogs | |
puts hash.notakey |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment