From: David Alan Black Date: 2002-01-20T21:03:01+09:00 Subject: Re: $_ as default parameter for a function Hello -- On Sun, 20 Jan 2002, Thomas wrote: > Could you, or someone else explain whats going on below? [...] > Mathieu Bouchard wrote in message > > > Yes, $_ is a local, and yes, built-in methods have access to the $_ of the > > caller. Here's an excerpt from MetaRuby 0.7 in module HollowString: > > > > # it's your own fault for using ~ > > def ~ > > # setting up quantum superposition > > r,w = IO.pipe > > if not fork then > > set_trace_func(proc{|*a| > > w.puts(eval("$_", a[4]).inspect) [...] The basic idea is that #set_trace_func takes a proc as an argument, and to that proc it provides an array of parameters when certain run-time events occur, such as returning from a method call. The fifth parameter (at [4]) is the binding at the time of the event. The idea is to eval $_ in the context of that binding (using the binding as the second argument to eval). Here's a little self-contained program that demonstrates this: def show_me my_proc = proc do |*params| print "Here's the $_ right after the 'event': " puts eval("$_.inspect",params[4]) end puts "Here's my $_: #{$_.inspect}" set_trace_func(my_proc) # Now the 'return' event (returning from this method) will # trigger the proc we've defined for #set_trace_func. end $_ = 100 show_me # Output: # Here's my $_: nil # Here's the $_ right after the 'event': 100 David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav