From: Jamis Buck Date: 2004-01-27T05:40:29+09:00 Subject: Re: def foo[](x) Gavin Kistner wrote: > #Works > class Foo1 > def [](x) > #... > end > end > > #Syntax Error > class Foo2 > def bar[](x) > #... > 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 [](x) ... 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"'