[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Yet Another Rite Thought: method combination

Christoph R.

11/18/2003 12:39:00 PM

ts wrote:
...
> 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
p "pre_b"
end
def foo:post
super
p "post_b"
end

def foo:wrap
super
p "B"
end
end


/Christoph


3 Answers

ts

11/18/2003 12:43:00 PM

0

>>>>> "C" == Christoph <chr_mail@gmx.net> writes:

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

C> class A

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

C> def foo:pre
C> super
^^^^^

We shall see this later :-)


Guy Decoux



matz

11/18/2003 5:03:00 PM

0

Hi,

I'd recommend you to check CLOS.

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

|> 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.


Joey Gibson

11/18/2003 9:00:00 PM

0

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

>Hi,
>
>I'd recommend you to check CLOS.
>
>

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? :-) )

; 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

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

http://www.joey...
http://www.joey.../blog/life/Wisdom.html