metaprogramming - Can a ruby object call super on a "grandparent"? -
if have class salad
method chew
, use super
in it, call next available chew
method available going chain of ancestors. if want "reach 2 levels up" (to second available chew
method going chain)? there way without adding super
first ancestor's method?
to make little more concrete, suppose salad
class, salad
object:
class salad < food include almonds include gorgonzola include spinach include dressing def chew super end end salad = salad.new
the array of ancestors this:
[salad, dressing, spinach, gorgonzola, almonds, food, object, kernel, basicobject]
if want salad.chew
trigger chew
method in spinach
without adding super
dressing#chew, possible? there way reach 2 levels (in same way super
"reaches" 1 level up)?
yes, there way describing, , way through methods #method
, #instance_method
, in method = spinach.instance_method( :chew )
, can bind salad, or metaclasses of salad instances, in method.bind( salad )
. mechanism not neat using convenience keyword super
, offers superset of super
's functionality. there few more details, trust me, way, can want. that's power of ruby – can anything, point lispiest magic, need ripper
, , (the far unfinished) sorcerer
.
(for usage example, see other answer @ using object.inspect / object.to_s on class derived hash)
Comments
Post a Comment