From: Robert Klemme Date: 2004-08-23T17:30:44+09:00 Subject: Re: Callback for instance var creation? "T. Onoma" schrieb im Newsbeitrag news:200408230415.32703.transami@runbox.com... > On Monday 23 August 2004 03:41 am, Robert Klemme wrote: > > Methods *are* first class objects: > > Yes, in one sense they are. That's true. But not in the sense that they > readily accessable and *carry state* (as in Python, FWIBT). That's the > important missing element. > > > > But constants are class-level while methods are actually instance level > > > (strangely enough). > > > > Not necessarily, see the first code example above (UnboundMethod). Note > > > > also that you can convert between the two types: > > >> Foo.new.method( :bar ).unbind > > > > => # > > > > >> Foo.instance_method( :bar ).bind( Foo.new ) > > > > => # > > Hmm... I see more clearly now how rebinding unbound methods "reinstates" the > method. But still, unbound methods seem useless. They can only be rebound to > the same kind of class. And you can query arity. Plus you can store anything that you like to in them (i.e. add state of your own). Just apply the pattern I presented for #method to Class#instance_method and you'll get the same instance each time you access a method. Note though that this approach doesn't take into consideration changes of the method definition *after* it was put into the hash. That might be a serious drawback depending on whether or not you need to cope with this. > Hmmm... but I do see that I might be able to finish my original proposal using > them. That might work. So the constant method would be the class-level > version. Oh, that might blend nicely with the capitalized methods for the > instance level version --they will look identical except for the parens. Personally I don't like at all introducing a constant for each method in a class. Plus, methods are not constant i.e. you can redefine them. What kind of state do you want to attach to methods? I mean, what problem are you trying to solve? robert