Yet Another Rite Thought: method combination

Can you give me an example with 2 classes (inheritance + wrappers) ?

class A

OK, but I want also the output, i.e. what you expect :-)))

  def foo:pre
    super

       ^^^^^

We shall see this later :slight_smile:

Guy Decoux

class Foo
  def bar
    puts "bar"
  end

     def bar:pre
        puts "pre"
     end

  def bar:wrap
    puts "pre"

    # invocation of bar here, no super class method involved,
    # hence "super" is inappropriate IMHO

and bar:pre is not called

Guy Decoux

Hi,

I’d recommend you to check CLOS.

Can you give me an example with 2 classes (inheritance + wrappers) ?

class A
def foo
p “A”
end
def foo:pre
p “pre_a”
end
def foo:post
p “post_a”
end

def foo:wrap
super
p “A”
end
end

class B < A
def foo
super
p “B”
end
def foo:pre

super # no super in pre method

p “pre_b”
end
def foo:post

super # no super in post method

p “post_b”
end

def foo:wrap
super
p “B”
end
end

B.new.foo

“pre_b”
“pre_a”
“A”
“B”
“post_a”
“post_b”
“A” # from wrap
“B” # from wrap

						matz.
···

In message “Re: Yet Another Rite Thought: method combination” on 03/11/18, “Christoph” chr_mail@gmx.net writes:

I don’t know if this will help or hinder someone’s understanding, but
here is some CLOS that mimics the Ruby2 actions (or is that the other
way around? :slight_smile: )

; define a pretty useless class
(defclass foo () ())

; define a method called ‘bar’ that just prints ‘inside-bar’
(defmethod bar ((x foo))
(print 'inside-bar))

; define a method that will get executed before 'bar
(defmethod bar :before ((x foo))
(print 'before-bar))

; define a method that will get executed after 'bar
(defmethod bar :after ((x foo))
(print 'after-bar))

; define a method that will wrap any call to 'bar
(defmethod bar :around ((x foo))
(print 'around-bar-before)
(call-next-method)
(print 'around-bar-after))

; create an instance of class ‘foo’ and assign it to slot ‘x’
(setq x (make-instance 'foo))

; call the bar method on our instance ‘x’
(bar x)

Yielding the following:

AROUND-BAR-BEFORE
BEFORE-BAR
INSIDE-BAR
AFTER-BAR
AROUND-BAR-AFTER

Note that in bar :around, the (call-next-method) is doing what Ruby2’s
proposed call to ‘super’ will do.

Does that help?
Joey

···

On 11/18/2003 12:02 PM, Yukihiro Matsumoto wrote:

Hi,

I’d recommend you to check CLOS.


Never trust a girl with your mother’s cow,
never let your trousers go falling down in the green grass…

def foo:pre
super

^^^^^

Okay, for pre's and post's I would expect
"normal" inheritance - this would also
be pretty useful IMO. In fact, this might
even become popular idiom.

You expect that super in B#foo:pre call A#foo:pre ?

This is this or it make another call ?

Guy Decoux

Joey Gibson wrote:

; call the bar method on our instance ‘x’
(bar x)

Yielding the following:

AROUND-BAR-BEFORE
BEFORE-BAR
INSIDE-BAR
AFTER-BAR
AROUND-BAR-AFTER

Note that in bar :around, the (call-next-method) is doing
what Ruby2’s proposed call to ‘super’ will do.

Does that help?

Thank you very much … I actually I did look
this up for myself but did not come acroos a
Reference on the leagility, effect usefull(or
more likely uselessness:-) of using a
call-next-method in a before or after method.

I actually sort of starting getting a clue that
Matz’s scheme (were super is disallowed in in pre’s
and post’s) but ``allowing multiple hook up
points’’ is pretty much equivalent in terms of power
to allowing super in pre’s and post’s but allowing
only one Hookup point. However if the criteria is
ease or consistency (for example the problem of
changing signature in sub and super class) it
seems that Matz scheme is clearly superior.

/Christoph

ts wrote:

Okay, for pre’s and post’s I would expect “normal”
inheritance - this
would also be pretty useful IMO. In fact, this might even become
popular idiom.

You expect that super in B#foo:pre call A#foo:pre ?

Yes

This is this or it make another call ?

Furthermore I would expect that the pre/ post
hooks are (and hence wrap buy association) are
only invoked at B’level - otherwise A#foo:pre/pst
would be called twice.

/Christoph