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 theNoMethodError
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
Similar Kata:
Stats:
Created | Jan 5, 2014 |
Published | Jan 5, 2014 |
Warriors Trained | 1125 |
Total Skips | 247 |
Total Code Submissions | 3083 |
Total Times Completed | 563 |
Ruby Completions | 547 |
Total Stars | 21 |
% of votes with a positive feedback rating | 90% of 49 |
Total "Very Satisfied" Votes | 41 |
Total "Somewhat Satisfied" Votes | 6 |
Total "Not Satisfied" Votes | 1 |