From: Mikael Brockman Date: 2004-10-13T01:00:56+09:00 Subject: Re: closure's self "trans. (T. Onoma)" writes: > Curious thought --- what if I'd like to know the arity of the proc being > executed? > > proc{|x| arity }.call(1) > > 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) Closures per se don't have selfs -- they're not objects. Consider | def frob &closure | [lambda &closure, lambda &closure] | end It gives two Procs with the same closure, but different selves. Here's an extremely ugly kludge: | class Proc | alias :real_call :call | | def call *args | old_proc = $current_proc | $current_proc = self | real_call *args | $current_proc = old_proc | end | end | | (lambda do |x| | puts $current_proc.arity | (lambda do |x, y| | puts $current_proc.arity | end).call nil, nil | puts $current_proc.arity | end).call nil (It prints 1, 2, 1.)