From: matz@... (Yukihiro Matsumoto) Date: 2004-01-27T10:10:51+09:00 Subject: Re: def foo[](x) Hi, In message "Re: def foo[](x)" on 04/01/27, Gavin Kistner writes: |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[](1)), 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. --- class ArefAttribute def initialize(&block) @block = block end def [](*args) @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[x] || @childrenByName[x] || @childrenById[x] } 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]