Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > mixins and extend (was: Attempted roadmap of future instance variables....)

Reply
Thread Tools

mixins and extend (was: Attempted roadmap of future instance variables....)

 
 
T. Onoma
Guest
Posts: n/a
 
      12-06-2003
Can anyone take a look at this and tell me what the hek is happening with
extend? (as in Pickaxe Chapter 19)

ruby-talk:87344

I thought I finally understood, but now I'm just confused again.

Thanks,
T.


Copy of 87344 follows:

On Friday 05 December 2003 08:26 pm, T. Onoma wrote:
> On Friday 05 December 2003 07:02 pm, Austin Ziegler wrote:
> > It is a mix-in.
> >
> > class A; end
> > class B; include Transaction::Simple; end
> > x = A.new
> > y = B.new
> > x.extend(Transaction::Simple)
> > class << x; self; end.ancestors
> > => [Transaction::Simple, A, Object, Kernel]
> > class << y; self; end.ancestors
> > => [B, Transaction::Simple, Object, Kernel]
> >
> > It's just that #extend mixes into the object's singleton class.

>
> Hmm... Well, you actually prove my point. Transaction::Simple is coming
> before A in the first case, and after B in the second. Quite different form
> the ordinary mixin-in. Yet you'll have to excuse my while I scream
> "Arrrrgggghhh!!!" b/c it is even more complex than I realized. So, yes, it
> is techinaclly a mix-in per se, but since it is a mix-in on the sinlgeton
> and not the class itself, it is more akin to a singleton. Or at least one
> would think! But it seems it is actually more akin to just
> redefining/restating the class:
>
> module M
> def happy; print "M"; super if defined?(super); end
> end
>
> class MC
> def happy; print "M"; super if defined?(super); end
> end
>
> class S < MC
> def happy; print "x"; super if defined?(super); end
> end
>
> class I
> include M
> def happy; print "x"; super if defined?(super); end
> end
>
> class E
> def happy; print "x"; super if defined?(super); end
> end
>
> class C
> def happy; print "x"; super if defined?(super); end
> end
>
> s = S.new
>
> a = I.new
>
> b = E.new
> b.extend(M)
>
> c = C.new
> def c.happy; print "M"; super if defined?(super); end
>
> print "superclass: "; s.happy
> print "\t", class << s; self; end.ancestors.inspect; puts
>
> print " include: "; a.happy
> print "\t", class << a; self; end.ancestors.inspect; puts
>
> print " extend: "; b.happy
> print "\t", class << b; self; end.ancestors.inspect; puts
>
> print " singleton: "; c.happy
> print "\t", class << c; self; end.ancestors.inspect; puts
>
> This produces:
>
> superclass: xM [S, MC, Object, Kernel]
> include: xM [I, M, Object, Kernel]
> extend: M [M, E, Object, Kernel]
> singleton: Mx [C, Object, Kernel]
>
> Notice in the singleton that "M" is invisible, but it does in fact exist
> anonymously before C.
>
> I have to say, I'm actually very disturbed to see the x missing in the
> extend. Where the heck did it go? Extending just redefined the happy method
> altogether! That's not something I would call "extend".
>
> T.




 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      12-06-2003
>>>>> "T" == T Onoma <(E-Mail Removed)> writes:

T> Can anyone take a look at this and tell me what the hek is happening with
T> extend? (as in Pickaxe Chapter 19)

Well, I've not understood your problem.

To simplify : by default (because it's possible to redefine what do ruby)
an extend can be seen as an include in the singleton class.

For your example

>> c = C.new
>> def c.happy; print "M"; super if defined?(super); end


[...]

>> singleton: Mx [C, Object, Kernel]
>>
>> Notice in the singleton that "M" is invisible, but it does in fact exist
>> anonymously before C.


M is not invisible : it just don't exist. You have just defined a method
#happy for `c', like the module M define a method #happy


Guy Decoux


 
Reply With Quote
 
 
 
 
T. Onoma
Guest
Posts: n/a
 
      12-06-2003
On Saturday 06 December 2003 04:31 pm, ts wrote:
> >>>>> "T" == T Onoma <(E-Mail Removed)> writes:

>
> T> Can anyone take a look at this and tell me what the hek is happening
> with T> extend? (as in Pickaxe Chapter 19)
>
> Well, I've not understood your problem.


Thanks for responding, Guy. I'll explain better.

> To simplify : by default (because it's possible to redefine what do ruby)
> an extend can be seen as an include in the singleton class.


"seen as an include in the singleton class": That's what confuses me. I can
"see" this by combing the diagrams on Pickaxe pg. 246 and 247, no problem.
But it dosen't explain to me why x isn't printed in my extend example.

(I wish there way a way I could draw a picture of it here)

> >> singleton: Mx [C, Object, Kernel]
> >>
> >> Notice in the singleton that "M" is invisible, but it does in fact exist
> >> anonymously before C.

>
> M is not invisible : it just don't exist. You have just defined a method
> #happy for `c', like the module M define a method #happy


Indeed I have defined that method, but it is put in a an unnamed class, i.e.
the singleton class. (Pickaxe Figure 19.2 on page 246, has a diagram of it.)
I'm simply refering to it as M, analogous to the rest of the examples. This
is why M is printed before x and dosen't simply replace x.

T.

[NOTE: I figured it out and it may be a bug in Ruby. At the the very least it
is a "bugaboo". I'll explain in next message -T.]





 
Reply With Quote
 
T. Onoma
Guest
Posts: n/a
 
      12-06-2003
Take a look at this:

class MC
def happy; print "C"; super if defined?(super); end
end

module M
def happy; print "M"; super if defined?(super); end
end

class S < MC
include M
def happy; print "x"; super if defined?(super); end
end

s = S.new

print "superclass: "; s.happy
print "\t", class << s; self; end.ancestors.inspect; puts

Outputs:

superclass: xM [S, M, MC, Object, Kernel]

Now look at this:

class MC
def happy; print "C"; super if defined?(super); end
end

module M
def happy; print "M"; super; end
end

class S < MC
include M
def happy; print "x"; super if defined?(super); end
end

s = S.new

print "superclass: "; s.happy
print "\t", class << s; self; end.ancestors.inspect; puts

Outputs:

superclass: xMC [S, M, MC, Object, Kernel]

The last is what one would expect. Seems that defined?(super) dosen't work
with mixins even though calling super does. What do you make of that? Bug?

T.




 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      12-06-2003
>>>>> "T" == T Onoma <(E-Mail Removed)> writes:

T> superclass: xM [S, M, MC, Object, Kernel]

svg% cat b.rb
#!./ruby -v
class MC
def happy; print "C"; super if defined?(super); end
end

module M
def happy; print "M"; super if defined?(super); end
end

class S < MC
include M
def happy; print "x"; super if defined?(super); end
end

s = S.new

print "superclass: "; s.happy
print "\t", class << s; self; end.ancestors.inspect; puts
svg%

svg% b.rb
ruby 1.8.1 (2003-11-30) [i686-linux]
superclass: xMC [S, M, MC, Object, Kernel]
svg%


Guy Decoux


 
Reply With Quote
 
T. Onoma
Guest
Posts: n/a
 
      12-06-2003
On Saturday 06 December 2003 05:16 pm, ts wrote:
> svg% b.rb
> ruby 1.8.1 (2003-11-30) [i686-linux]
> superclass: xMC [S, M, MC, Object, Kernel]
> svg%


So what do you know:

ruby 1.8.0 (2003-08-04) [i686-linux]
superclass: xM [S, M, MC, Object, Kernel]

Cool, so it's been fixed! And I do understand correctly, and I have all along.
Guess it's time to upgrade.

Whew! That really scared the hek out of me. My world started to come tumbling
down. But super Guy was there to keep the sky from falling

Thanks, Guy.
T.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free Webinars on PMP Certification Awareness and Roadmap makarand VHDL 0 07-07-2008 01:14 PM
Free Webinars on PMP Certification Awareness and Roadmap makarand Perl Misc 0 07-04-2008 05:26 AM
Instance Variable in Mixins Dale Martenson Ruby 1 05-19-2008 11:36 PM
3/30/05 NYJavaSIG Meeting - "J2SE Performance Tips for JDK 5.0 and NetBeans Roadmap for the Future" Frank D. Greco Java 0 03-22-2005 08:24 AM
Attempted roadmap of future instance variables.... David A. Black Ruby 1 12-03-2003 04:41 PM



Advertisments