Aquarius
1/29/2009 3:11:00 PM
[Note: parts of this message were removed to make it a legal post.]
And I have to override Array#each. I'll also need to override #map, #select
and so on. Not a good deal :(
On Tue, Jan 27, 2009 at 7:46 PM, Robert Klemme
<shortcutter@googlemail.com>wrote:
> 2009/1/27 Stefan Kanev <stefan.kanev@gmail.com>:
> > 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? :)
>
> IMHO you can't because the current value is only known by method
> #each. Wait, you could cook something up using thread local variables
>
> 18:41:58 tmp$ ./it.rb
> 4
> 6
> 5
> 18:42:41 tmp$ cat it.rb
> #!/bin/env ruby
>
> def it
> Thread.current[:each].last rescue nil
> end
>
> class Array
> alias _each each
> def each(&b)
> stack = Thread.current[:each] ||= []
> _each do |val|
> stack.push val
> begin
> b.call
> ensure
> stack.pop
> end
> end
> end
> end
>
> %w{more chunky bacon}.each { puts it.length }
> 18:42:42 tmp$
>
> But note that you have to change *all* implementations of #each which
> is difficult to achieve because classes can spring into existence all
> the time. Alternatively write a global each which receives the
> Enumerable as argument.
>
> Cheers
>
> robert
>
> --
> remember.guy do |as, often| as.you_can - without end
>
>