From: Stefan Kanev Date: 2009-01-27T23:30:04+09:00 Subject: Re: How can I get the arguments passed to the caller. --00504502cd72dd24f1046177b8dc Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Thanks for the advice. I would have never had thought of something as straightforward as evaling 'local_variables' in the binding. Unfortunatelly, I believe that this doesn't yield the arguments if they are not named. I'm actually trying, out of curiosity, to implement a very straightforward idea: %w{more chunky bacon}.each { puts it.length } Like, have *it* to actually be the 'implicit blog argument', not unlike perl's $_. Again, I'm doing that out of curiosity. Any ideas how I can accomplish it without touching the interpreter's C code? :) On Mon, Jan 26, 2009 at 10:35 AM, Brian Candler wrote: > James Coglan wrote: > >> first_arg = .... > >> $stuff << first_arg > >> end > >> > >> bar(42) > >> > >> # By this point, $stuff would be [42] > > > > > > > > I think the closest you're going to get is for the calling function to > > pass > > its binding so that foo can read from it: > > In which case, it might as well just pass its args instead :-) > > There is the Binding.of_caller hack which you can find through a google > search. > > Maybe you could combine this with Kernel#local_variables or > Binding#local_variables to do what you want? That's what debug.rb does > > when /^\s*l(?:ocal)?\s*$/ > var_list(eval("local_variables", binding), binding) > > and just use eval to read them: > > def var_list(ary, binding) > ary.sort! > for v in ary > stdout.printf " %s => %s\n", v, eval(v, binding).inspect > end > end > -- > Posted via http://www.ruby-forum.com/. > > --00504502cd72dd24f1046177b8dc--