I want to write a method in ruby to replace a current method that simply returns a hash.
So currently we have this
def some_stats
{
:foo => "bar",
:zoo => "zar"
}
end
which can be called like this:
some_stats[:foo]
I want to replace this with a method that is called with :foo, which does some more complicated things that just building a hash. However, I'd like it to be called in the same way, with the square bracket notation. EG
#this is a bit pseudo-cody but hopefully you get the idea.
def some_stats[](key)
if key.to_s =~ /something/
#do something
return something
else
#do something else
return something_else
end
end
This would still be called like some_stats[:foo]
I can't work out the syntax for defining the method name: the above, def some_stats[](key) doesn't work. I feel like this should be possible though.