On Thu, Mar 21, 2013 at 1:20 PM, Marcin R. <lists / ruby-forum.com> wrote:
> Robert Klemme wrote in post #1102581:
>> On Thu, Mar 21, 2013 at 11:31 AM, Marcin R. <lists / ruby-forum.com>
>> wrote:
>
>> I usually prefer a written explanation of the purpose of the code.
>> The code is the solution but the goal does not always become apparent
>> from looking at code.
>
> So true, but I am hoping my explanation given below was sufficient.

>> Why would I want to do that?  What is that supposed to do?
>
> As I mentioned previously my goal is to create easy to use
> implementation of chain pattern.

Are you talking about "chain of responsibility"?
http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
http://c2.com/cgi/wiki?ChainOfResponsibilityPattern

> You can do it manually, calling
> next-in-line via some sort of reference you keep ("next" in my case),
> but I feel that we can do better than this. My idea is to keep
> boilerplate in chain module and let implementations yield (because
> that's quite natural way of doing things like this in Ruby - you
> temporarily yield control to regain it later) when they need to pass the
> control down the chain.

I don't see the advantage of using "yield" over any other method name.
 It seems you are going through hoops just to allow yield to delegate
to another instance.  I don't think it's worthwhile.  For me this
would be good enough:

ChainedBase = Struct.new :delegate do
  def chain(*a, &b)
    m = caller[0][%r{`(.*?)'\z}, 1]
    d = delegate and d.send(m, *a, &b)
  end
end

class Der < ChainedBase
  def foo x
    puts "entry Der"
    chain x
    puts "exit  Der"
  end
end

class Catcher
  def method_missing(m, *a, &b)
    printf "Caught: %s(%s)\n", m, a.map(&:inspect).join(', ')
  end
end

o = Der.new
o.delegate = Catcher.new
o.foo 123

>>> If call is recursive than it is either 'super' call or plain recursion,
>>
>> Super call is not recursion.  Recursion is when a method invokes
>> itself.  Super calls are used to augment a super class's
>> implementation of a method with additional logic specific to the
>> derived class.
>
> Yes, so I said - EITHER super OR recursive.

Yeah, but the fact that you mix them together is suspicious.  Chain of
responsibility is all about (conditional) delegation to another
instance.  Calling superclass methods does not fit that pattern
because it is the *same* instance.

> Hope I've already made myself clear.

It seems so.

> Thanks for your input.

You're welcome

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/