From: Ara.T.Howard@... Date: 2004-10-13T01:04:41+09:00 Subject: Re: closure's self On Wed, 13 Oct 2004, trans. (T. Onoma) wrote: > Curious thought --- what if I'd like to know the arity of the proc being > executed? > > proc{|x| arity }.call(1) like this? harp:~ > irb irb(main):001:0> cb = proc{|a,b,c| cb.arity} => # irb(main):002:0> p cb.call(0,1,2) 3 => nil > I see no way to do this, not even with the extended Binding class. I begin to > wonder (for this and other reasons) if a closure's "self" is doable? (at > least I think _closure_ is the right "scope" for this) > > proc{|x| this.arity }.call(1) i don't quite understand this - but i think 'self' must be Object, although you could probably subclass Proc in a way that it used the 'with' trick or something, in any case it seems you don't really need 'self' if you can name it some other way. maybe you could make a subclass that regeistered itself as a class var when being called? something like harp:~ > cat b.rb class CallBack < Proc class << self attr :this, true end def call(*a, &b) begin self.class.this = self super ensure self.class.this = nil end end end CallBack::new{|a| p CallBack::this.arity }.call(42) #=> 1 if you get my meaning? cheers. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================