For the instances of a class that I'm writing (an ActiveRecord model), I'd like to be able to overload assignments like this:
m.rank['foo'] = 1
m.rank['bar'] = 2
In other words, I don't want the numbers to be written into an actual @rank hash, but rather I'd like to have some setter method called with 'foo' and 1 as its parameters.
A naive way to get this functionality would be to define set_rank(key, rank) and get_rank(key), but the syntax isn't very sugary. For nicer syntax, one could define a helper class that defines [] and []=, and have the rank method return an instance of that class.
Is there an established pattern to write this in a concise manner? Or is it a bad idea in the first place, and I should just use set_rank and get_rank?