# Def foo\[\](x)

**URL:** <https://rubytalk.org/t/def-foo-x/9859>\
**Category:** ruby-talk\
**Created:** [26 January 2004 19:54 UTC](https://rubytalk.org/t/def-foo-x/9859 "2004-01-26T19:54:54Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [26 January 2004 19:54 UTC](https://rubytalk.org/t/def-foo-x/9859/1 "2004-01-26T19:54:54Z")

</div>

#Works  
class Foo1  
def   
#…  
end  
end

#Syntax Error  
class Foo2  
def bar  
#…  
end  
end

Is there no way to define a method that has a name and [] in it, to be  
able to do something like:

myObj = Foo.new  
puts myObj.bar[12]

…without having to create a new class for bar and define the [] method  
in that class?

---

<div class="post-metadata">

**Author:** ![Laurent\_Julliard4](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@Laurent\_Julliard4](https://rubytalk.org/u/Laurent_Julliard4)\
**Post date:** [26 January 2004 20:34 UTC](https://rubytalk.org/t/def-foo-x/9859/2 "2004-01-26T20:34:53Z")

</div>

> #Works  
> class Foo1  
> def   
> #…  
> end  
> end
> 
> #Syntax Error  
> class Foo2  
> def bar  
> #…  
> end  
> end
> 
> Is there no way to define a method that has a name and in it, to be  
> able to do something like:
> 
> myObj = Foo.new  
> puts myObj.bar[12]
> 
> …without having to create a new class for bar and define the method  
> in that class?

> > class C  
> > def initialize  
> > @bar=method :met  
> > end  
> > def met(x)  
> > x\*\*2  
> > end  
> > private :met  
> > attr\_reader :bar  
> > end  
> > =\> nil  
> > C.new.bar[12]  
> > =\> 144

alla callable objects (proc, method, continuations have a method.  
But I’m not sure this is what you would see 🙂

> **···**
>
> il Mon, 26 Jan 2004 19:50:26 GMT, Gavin Kistner [gavin@refinery.com](mailto:gavin@refinery.com) ha scritto::

---

<div class="post-metadata">

**Author:** ![Jamis\_Buck4](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@Jamis\_Buck4](https://rubytalk.org/u/Jamis_Buck4)\
**Post date:** [26 January 2004 20:40 UTC](https://rubytalk.org/t/def-foo-x/9859/3 "2004-01-26T20:40:29Z")

</div>

Gavin Kistner wrote:

> #Works  
> class Foo1  
> def   
> #…  
> end  
> end
> 
> #Syntax Error  
> class Foo2  
> def bar  
> #…  
> end  
> end
> 
> Is there no way to define a method that has a name and in it, to be  
> able to do something like:
> 
> myObj = Foo.new  
> puts myObj.bar[12]
> 
> …without having to create a new class for bar and define the   
> method in that class?
> 
> .

Well… you could so this:

class Foo2  
def   
…  
end

```
 def bar
     self
 end

```

end

myObj = Foo.new  
puts myObj.bar[12]

But then you’re limited to one such attribute per class. Other than  
that, I know of no way to do what you described, aside from (as you  
mentioned) creating a different class that defines , and then having  
an instance of that class associated with the bar attribute of Foo2. ;(

- Jamis

> **···**
>
> –  
> Jamis Buck  
> jgb3@email.byu.edu
> 
> ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S\*)|le.l(…)e|#!(\S\*)/) {|r| a \<\< r.compact.first };puts “\n\>#{a.join(%q/ /)}\<\n\n”’

---

<div class="post-metadata">

**Author:** ![David\_Heinemeier\_Ha2](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@David\_Heinemeier\_Ha2](https://rubytalk.org/u/David_Heinemeier_Ha2)\
**Post date:** [26 January 2004 20:41 UTC](https://rubytalk.org/t/def-foo-x/9859/4 "2004-01-26T20:41:56Z")

</div>

> …without having to create a new class for bar and define the   
> method in that class?

I’ve been puzzled by this myself. There has been many times where I’ve  
wanted to fake the appearance of a collection within a class without  
actually making another class just for the purpose.

Could this perhaps make it in as an RCR?

> **···**
>
> –  
> David Heinemeier Hansson.  
> [http://www.loudthinking.com/](http://www.loudthinking.com/) – Broadcasting Brain

---

<div class="post-metadata">

**Author:** ![Dan\_Doel](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dan\_Doel](https://rubytalk.org/u/Dan_Doel)\
**Post date:** [26 January 2004 21:41 UTC](https://rubytalk.org/t/def-foo-x/9859/5 "2004-01-26T21:41:48Z")

</div>

Acutally, this might be better:

instead of:

class Foo  
def bar  
# code  
end  
end

do:

class Foo  
def bar  
proc { |\*args|  
# code  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [26 January 2004 22:19 UTC](https://rubytalk.org/t/def-foo-x/9859/6 "2004-01-26T22:19:54Z")

</div>

David Heinemeier Hansson wrote:

> I’ve been puzzled by this myself. There has been many times where I’ve  
> wanted to fake the appearance of a collection within a class without  
> actually making another class just for the purpose.
> 
> Could this perhaps make it in as an RCR?

I’m glad I’m not the only one. I have now added this as an RCR:  
[http://www.wobblini.net/rcr/RCR/RCR201](http://www.wobblini.net/rcr/RCR/RCR201)

Please check it out and vote! 🙂

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [26 January 2004 22:39 UTC](https://rubytalk.org/t/def-foo-x/9859/7 "2004-01-26T22:39:00Z")

</div>

Gavin Kistner wrote:

> David Heinemeier Hansson wrote:
> 
> > I’ve been puzzled by this myself. There has been many times where I’ve  
> > wanted to fake the appearance of a collection within a class without  
> > actually making another class just for the purpose.
> > 
> > Could this perhaps make it in as an RCR?
> 
> I’m glad I’m not the only one. I have now added this as an RCR:  
> [http://www.wobblini.net/rcr/RCR/RCR201](http://www.wobblini.net/rcr/RCR/RCR201)

Question: in this method call:

x.foo[1]

what is the search order? First for #foo, and then for #foo? Or the  
other way around?

Suppose it’s #foo, then #foo. What if #foo is implemented in the  
superclass, but #foo is implemented in the class of x itself? In other  
words, depth-first search or breadth-first search?

It seems difficult to resolve the ambiguities inherent in this notation.  
(But I have sometimes wished this feature existed.)

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [27 January 2004 09:39 UTC](https://rubytalk.org/t/def-foo-x/9859/8 "2004-01-27T09:39:55Z")

</div>

“Gavin Kistner” [gavin@refinery.com](mailto:gavin@refinery.com) schrieb im Newsbeitrag  
news:QkgRb.157054$xy6.751016@attbi\_s02…

> David Heinemeier Hansson wrote:
> 
> > I’ve been puzzled by this myself. There has been many times where I’ve  
> > wanted to fake the appearance of a collection within a class without  
> > actually making another class just for the purpose.
> > 
> > Could this perhaps make it in as an RCR?
> 
> I’m glad I’m not the only one. I have now added this as an RCR:  
> [http://www.wobblini.net/rcr/RCR/RCR201](http://www.wobblini.net/rcr/RCR/RCR201)
> 
> Please check it out and vote! 🙂

-1 (voting on the site didn’t work for me)

Could any of the proposers of this feature explain, why it is needed? And  
why especially the proposed solution that used self for this isn’t  
sufficient, i.e., why one would need several of those “virtual” members?

Regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [26 January 2004 23:14 UTC](https://rubytalk.org/t/def-foo-x/9859/9 "2004-01-26T23:14:53Z")

</div>

Joel VanderWerf wrote:

> > I’m glad I’m not the only one. I have now added this as an RCR:  
> > [http://www.wobblini.net/rcr/RCR/RCR201](http://www.wobblini.net/rcr/RCR/RCR201)
> 
> Question: in this method call:
> 
> x.foo[1]
> 
> what is the search order? First for #foo, and then for #foo? Or the  
> other way around?

I addressed this in the RCR.

Currently the following are equivalent:  
x.foo[1]  
(x.foo)[1]  
(x.foo)

I propose that it try:  
x.foo  
first, and if that method does not exist, then default to the current:  
(x.foo)

> Suppose it’s #foo, then #foo. What if #foo is implemented in the  
> superclass, but #foo is implemented in the class of x itself? In other  
> words, depth-first search or breadth-first search?

Hrm…this is a good point. You’re saying that with the following, the  
syntax would allow both, but there would be ambiguity:

```
class Foo
	def initialize
		@beernuts=[]
		@peanuts=[]
	end
	def bar
		return @peanuts
	end
	def bar[](x)
		return @beernuts[x]
	end
end

```

x = Foo.new  
puts x.bar[0] #Is this @peanuts[0] or @beernuts[0]

Adding both would be foolish as a developer (perhaps even throwing a  
warning about them at compile time), but as long as the search order was  
clearly defined, I personally wouldn’t be too worried.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [26 January 2004 23:29 UTC](https://rubytalk.org/t/def-foo-x/9859/10 "2004-01-26T23:29:17Z")

</div>

Gavin Kistner wrote:  
…

> I propose that it try:  
> x.foo  
> first, and if that method does not exist, then default to the current:  
> (x.foo)
> 
> > Suppose it’s #foo, then #foo. What if #foo is implemented in the  
> > superclass, but #foo is implemented in the class of x itself? In  
> > other words, depth-first search or breadth-first search?
> 
> Hrm…this is a good point. You’re saying that with the following, the  
> syntax would allow both, but there would be ambiguity:
> 
> ```
> class Foo
> def initialize
> @beernuts=[]
> @peanuts=[]
> end
> def bar
> return @peanuts
> end
> def bar[](x)
> return @beernuts[x]
> end
> end
> 
> ```
> 
> x = Foo.new  
> puts x.bar[0] #Is this @peanuts[0] or @beernuts[0]

Your rule above, as I read it, would resolve the ambiguity: @beernuts[0].

Trying #bar before #bar, as you propose, makes sense because if you  
really want #bar, you can easily force it with parens: (x.bar)[0],  
whereas there’s no way to force #bar, short of using #send.

But what I had in mind was something involving inheritance:

class Base  
def bar  
“base”  
end  
end

class Sub \< Base  
def bar(x)  
[“sub”]  
end  
end

Sub.new.bar[0]

Would this return “base” or “sub”?

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [26 January 2004 23:29 UTC](https://rubytalk.org/t/def-foo-x/9859/11 "2004-01-26T23:29:55Z")

</div>

Gavin Kistner wrote:

> Joel VanderWerf wrote:
> 
> > Suppose it’s #foo, then #foo. What if #foo is implemented in the  
> > superclass, but #foo is implemented in the class of x itself? In  
> > other words, depth-first search or breadth-first search?
> 
> Adding both would be foolish as a developer (perhaps even throwing a  
> warning about them at compile time), but as long as the search order was  
> clearly defined, I personally wouldn’t be too worried.

To be more clear (since I only realized what you meant halfway  
through)…I’m not sure which makes more sense. It’s sort of like  
7+3\*2  
which should come first, addition or multiplication? By convention,  
multiplication, but there’s not (AFAIK) a strong reason for it. That’s  
just how it is, and once you know that, you know what to expect.

If I had to pick one, I suppose I’d choose breadth first; the most  
recently/closely defined method should be used.

But this does point out a potential confusing point in the RCR, and  
probably explains why it hasn’t been included in the language  
already…not oversight, but purposeful omission.

> **···**
>
> –  
> (-, /\ / / //

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [27 January 2004 01:10 UTC](https://rubytalk.org/t/def-foo-x/9859/12 "2004-01-27T01:10:51Z")

</div>

Hi,

> To be more clear (since I only realized what you meant halfway  
> through)…I’m not sure which makes more sense. It’s sort of like  
> 7+3\*2  
> which should come first, addition or multiplication? By convention,  
> multiplication, but there’s not (AFAIK) a strong reason for it. That’s  
> just how it is, and once you know that, you know what to expect.

The example above (7+3\*2) is just a matter of precedence, but this one  
(say, obj.foo[1]) cannot be resolved by precedence; there are two  
possible interpretation ((obj.foo)[1] and obj.foo), and worse,  
you can tell them only by trying. I don’t like this kind of  
ambiguity.

The problem shown in the RCR can be solved like the following. And I  
feel this is far better solution for this particular problem.

```
						matz.

```

> **···**
>
> In message “Re: def foo” on 04/01/27, Gavin Kistner [gavin@refinery.com](mailto:gavin@refinery.com) writes:
> 
> * * *
> 
> class ArefAttribute  
> def initialize(&block)  
> @block = block  
> end  
> def   
> @block.call(\*args)  
> end  
> end
> 
> class Module  
> def attr\_aref(name, &body)  
> name = name.to\_s  
> accr = name+“\_accessor”  
> define\_method(accr, &body)  
> define\_method(name) {  
> ArefAttribute.new {|\*args|  
> self.send(accr, \*args)  
> }  
> }  
> end  
> end
> 
> class HTMLElement  
> attr\_aref(:children) {|x|  
> @childElements || @childrenByName || @childrenById  
> }  
> def initialize  
> @childElements={1=\>2}  
> @childrenByName={2=\>4}  
> @childrenById={4=\>8}  
> end  
> end
> 
> h = HTMLElement.new  
> p h.children[1]  
> p h.children[2]  
> p h.children[4]  
> p h.children[0]

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [27 January 2004 15:10 UTC](https://rubytalk.org/t/def-foo-x/9859/13 "2004-01-27T15:10:00Z")

</div>

Yukihiro Matsumoto wrote:

> […] I don’t like this kind of ambiguity.
> 
> The problem shown in the RCR can be solved like the following. And I  
> feel this is far better solution for this particular problem.

Thanks for your consideration of the problem, and thanks for the source  
code solution. That’s relatively generic and useful. 🙂

I’ve changed my vote to oppose my own RCR, and am happy to have it  
rejected 🙂

(Matz, you commented on the RCR: “Three consequent objection might  
discourage you, but it always happens in the design process of a  
language.” I am not at all discouraged. I’m happy that those who  
understand the language better than I were able to find flaws with my  
proposal and keep from adding to the language something that would make  
it less clean. The process works…yay! 🙂

> **···**
>
> –  
> (-, /\ / / //

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [27 January 2004 16:28 UTC](https://rubytalk.org/t/def-foo-x/9859/14 "2004-01-27T16:28:01Z")

</div>

Hi,

> I’ve changed my vote to oppose my own RCR, and am happy to have it  
> rejected 🙂

You can withdraw it by yourself. I’m not going to reject any RCR  
until the end of March if it provides all information needed.

> (Matz, you commented on the RCR: “Three consequent objection might  
> discourage you, but it always happens in the design process of a  
> language.” I am not at all discouraged. I’m happy that those who  
> understand the language better than I were able to find flaws with my  
> proposal and keep from adding to the language something that would make  
> it less clean. The process works…yay! 🙂

I appreciate your understanding. Last 10 years so many ideas have  
passed by in my brain, and I had to reject most of them by myself.

```
						matz.

```

> **···**
>
> In message “Re: def foo” on 04/01/28, Gavin Kistner [gavin@refinery.com](mailto:gavin@refinery.com) writes:
