Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby call "super" from a block (i.e in context)

Suppose I have:

class A
  include B
  include C
end

module B
  def test_method
    puts "Executed second"
  end
end

module C
  def super_calling 
    proc { super }  
  end

  def test_method
    "Executed first"
    super_calling.call
  end
end

I expected to execute the block proc { super } in context of the function C::test_method, so that B::test_method would have been invoked, but I get an error that says something like: "super_calling has not superclass" or something like that (don't remember exactly).

Everything works fine (no wonder) if C::test_method is defined like this:

def test_method
  "Executed first"
   super
end

My question is - I'm asking too much from Ruby or there is something I don't understand? Can someone shed light on it?

like image 646
Dmitri Avatar asked Feb 01 '26 13:02

Dmitri


1 Answers

Remove whole super_calling and just use super from within the C::test_method.

It will call B::test_method at that point.

like image 132
Vjatseslav Gedrovits Avatar answered Feb 04 '26 05:02

Vjatseslav Gedrovits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!