5 kyu

Improving hashes.

547 of 563kelvinst

Description:

Hi!

Well, you probably already are familiar with hashes. But, well, I want to access hash values through a diferent way, I really do not like to use the [] method.

Here's how I want to access the values:

hash = {a: 2, b: 1, c: 0.5}

hash.a # must return the same as hash[:a]
hash.b # must return the same as hash[:b]
hash.c # must return the same as hash[:c]

But there is some things that you may be aware:

  • If the hash already has a method with the key name, the existent method must be kept (e.g. {size: 5}.size must return the hash size, and not the value)
  • If the hash doens't include the key, you must return the that call an inexistent method in hash (e.g. hash = {a: 1}; hash.b must throw the NoMethodError exception)
  • String keys should work as well (e.g. {'a' => 'b'}.a must return 'b')

Some tips:

  • You must reopen the Hash class
  • Try to do with the less code as possible
Metaprogramming
Refactoring

Stats:

CreatedJan 5, 2014
PublishedJan 5, 2014
Warriors Trained1125
Total Skips247
Total Code Submissions3083
Total Times Completed563
Ruby Completions547
Total Stars21
% of votes with a positive feedback rating90% of 49
Total "Very Satisfied" Votes41
Total "Somewhat Satisfied" Votes6
Total "Not Satisfied" Votes1
Ad
Contributors
  • kelvinst Avatar
  • jhoffner Avatar
Ad