David L Altenburg
3/20/2008 10:26:00 PM
Howdy,
On Mar 20, 2008, at 4:56 PM, Alex Wayne wrote:
> I am trying to define a method that you call like this:
>
> @obj.foo[123]
>
> I thought I knew how to do this. I tried to setup the method like so:
>
> def foo[](value)
> value * 2
> end
>
Close. You actually want to override the method named "[]" (and maybe
"[]=").
So, something like this:
class Foo
def [](index)
# logic here
end
def []=(index, value)
# do some stuff
end
end
So, for the call you want to make work:
> @obj.foo[123]
@obj.foo needs to return an object with "[]" defined.
HTH,
David