Skip to content

Instantly share code, notes, and snippets.

@sinankeskin
Last active August 29, 2015 14:05
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 sinankeskin/89e7a3eb98943138395b to your computer and use it in GitHub Desktop.
Save sinankeskin/89e7a3eb98943138395b to your computer and use it in GitHub Desktop.
Hash Key'lerini metod ismi ile çağırmak - 2 - İyileştirme
class Hash
def method_missing(method_name)
if not respond_to?(method_name)
if has_key?(method_name)
self[method_name]
else
super.method_missing(method_name)
end
end
end
end
hash = {ad: 'Sinan', soyad: 'Keskin'}
puts hash.ad # Sinan
puts hash.adres # test.rb:7:in `method_missing': undefined method `adres' for {:ad=>"Sinan", :soyad=>"Keskin"}:Hash (NoMethodError)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment