From: Ilias Lazaridis Date: 2011-06-05T14:55:28+09:00 Subject: Re: CORE - Altering Behaviour of "each do" (default param "item") On 4 Éïýí, 21:02, Robert Klemme wrote: > On 04.06.2011 16:19, Michael Sokol wrote: > > > Actually it would be possible, although very ugly. > > > You need to evaluate in the context of the current instance > > (instance_eval) - not yield - the block you pass to the each method > > of your object (an array in this case). > > > Then you can define a print method that behaves like a coroutine > > (check out ruby fibers) and returns each element of your array. > > There is another approach which does not suffer from the side effect of > changing self which can be disastrous: create a thread local which holds > a stack of values and make item peek at the top element of it: > > class Object >    def new_each >      stack = (Thread.current[:__item__] ||= []) what does "(Thread.current[:__item__] ||= [])" do? >      each do |x| >        stack.push x [...] > This approach > - does not change 'self' > - is thread safe > - is nesting safe This sounds good. But you suggestion adds Class#new_each. The goal was, to alter the existent behaviour of Array#each. I've tried to modify your code to achieve this, but I failed. . -- http://lazaridis.com