# Delcaring a foo\[\] method

**URL:** <https://www.ruby-forum.com/t/delcaring-a-foo-method/132512>\
**Category:** Ruby\
**Created:** [March 20, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/delcaring-a-foo-method/132512 "2008-03-20T22:56:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [March 20, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/delcaring-a-foo-method/132512/1 "2008-03-20T22:56:31Z")

</div>

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 \* 2  
end

But this gives me a syntax error right at that first brace. I could  
have sworn this worked before. What am I doing wrong?

ruby 1.8.6

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [March 20, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/delcaring-a-foo-method/132512/2 "2008-03-20T23:23:13Z")

</div>

On Thu, Mar 20, 2008 at 4:56 PM, Alex W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to define a method that you call like this:
> 
> @obj.foo[123]
> 
> I thought I knew how to do this…

For this to work, the “foo” method must return an instance of an  
object that defines a “[]” method. So, for example, something like  
this:

```
class IndexableThing
  def [](index)
    2*index
  end
end

class OtherThing
  def foo
    IndexableThing.new
  end
end

obj = OtherThing.new
obj.foo[2] # => returns 4

```

Hope this helps,

Lyle

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [March 20, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/delcaring-a-foo-method/132512/3 "2008-03-20T23:27:19Z")

</div>

Alex W. wrote:

> I am trying to define a method that you call like this:
> 
> Â @obj.foo[123]

That’s two method calls: it calls foo() on @obj and then on the  
result  
of foo(). So you need to define foo to return something that responds to  
[].  
Like this:

def foo()  
Object.new.instance\_eval do  
def   
value\*2  
end  
self  
end  
end

foo  
#=\> #Object:0x2b47ef9898a8

foo[6]  
#=\> 12

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [March 21, 2008, 12:24am UTC](https://www.ruby-forum.com/t/delcaring-a-foo-method/132512/4 "2008-03-21T00:24:59Z")

</div>

David L Altenburg wrote:

> class Foo
> 
> def   
> # logic here  
> end
> 
> def []=(index, value)  
> # do some stuff  
> end
> 
> end

Doh, ok. That makes sense. [] cant be part of a method name unless it  
IS the whole method.

Thanks guys.

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [March 20, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/delcaring-a-foo-method/132512/5 "2008-03-20T23:29:41Z")

</div>

Howdy,

On Mar 20, 2008, at 4:56 PM, Alex W. 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 \* 2  
> end

Close. You actually want to override the method named “[]” (and maybe  
“[]=”).

So, something like this:

class Foo

def   
# 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
