ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* extend and super -- I cannot understand why this behavior
@ 2005-10-10 19:52 TRANS
  2005-10-10 20:28 ` Mathieu Bouchard
  0 siblings, 1 reply; 7+ messages in thread
From: TRANS @ 2005-10-10 19:52 UTC (permalink / raw
  To: ruby-core

  module Q
    module H
      def x ; @x ||= [] ; end
    end
    extend H
  end
  => Q

  class R
    extend Q::H
    def self.x ; @x ||= [] ; super + @x  ; end
    def self.x! ; @x ||= [] ; end
  end
  => nil

  Q.x
  => []

  Q.x << 1
  => [1]

  R.x
  => []

  R.x! << 2
  => [2]

  R.x
  => [2, 2]  # huh?

  Q.x
  => [1]

T.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: extend and super -- I cannot understand why this behavior
  2005-10-10 19:52 extend and super -- I cannot understand why this behavior TRANS
@ 2005-10-10 20:28 ` Mathieu Bouchard
  2005-10-10 21:01   ` TRANS
  0 siblings, 1 reply; 7+ messages in thread
From: Mathieu Bouchard @ 2005-10-10 20:28 UTC (permalink / raw
  To: ruby-core

On Tue, 11 Oct 2005, TRANS wrote:

>   module Q
>     module H
>       def x ; @x ||= [] ; end
>     end
>     extend H
>   end
>   class R
>     extend Q::H
>     def self.x ; @x ||= [] ; super + @x  ; end
>     def self.x! ; @x ||= [] ; end
>   end

>   R.x! << 2
>   => [2]
>   R.x
>   => [2, 2]  # huh?

class<<R;self;end is a subclass of class<<Q::H;self;end, right?

classes are objects like everything else. Calling super causes a 
supermethod to be called in class<<Q::H;self;end with self=R.

____________________________________________________________________
Mathieu Bouchard - tél:+1.514.383.3801 - http://artengine.ca/matju
Freelance Digital Arts Engineer, Montréal QC Canada

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: extend and super -- I cannot understand why this behavior
  2005-10-10 20:28 ` Mathieu Bouchard
@ 2005-10-10 21:01   ` TRANS
  2005-10-10 23:26     ` Mathieu Bouchard
  0 siblings, 1 reply; 7+ messages in thread
From: TRANS @ 2005-10-10 21:01 UTC (permalink / raw
  To: ruby-core

On 10/10/05, Mathieu Bouchard <matju@artengine.ca> wrote:
> On Tue, 11 Oct 2005, TRANS wrote:
>
> >   module Q
> >     module H
> >       def x ; @x ||= [] ; end
> >     end
> >     extend H
> >   end
> >   class R
> >     extend Q::H
> >     def self.x ; @x ||= [] ; super + @x  ; end
> >     def self.x! ; @x ||= [] ; end
> >   end
>
> >   R.x! << 2
> >   => [2]
> >   R.x
> >   => [2, 2]  # huh?
>
> class<<R;self;end is a subclass of class<<Q::H;self;end, right?
>
> classes are objects like everything else. Calling super causes a
> supermethod to be called in class<<Q::H;self;end with self=R.

Ah, I see. So it's the same @x in either method --I thinking super
would behave the same as calling Q::H.x. Is this why super is "hard
bound" at compile time.

Thanks,
T.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: extend and super -- I cannot understand why this behavior
  2005-10-10 21:01   ` TRANS
@ 2005-10-10 23:26     ` Mathieu Bouchard
  2005-10-10 23:59       ` TRANS
  0 siblings, 1 reply; 7+ messages in thread
From: Mathieu Bouchard @ 2005-10-10 23:26 UTC (permalink / raw
  To: ruby-core

On Tue, 11 Oct 2005, TRANS wrote:
> Ah, I see. So it's the same @x in either method --I thinking super would
> behave the same as calling Q::H.x. Is this why super is "hard bound" at
> compile time.

What do you mean by hard-bound at compile time? In a super-call, the self 
of the callee is always the self of the caller. However the 
classes/modules involved are not necessarily predetermined:

  module A; def foo; "A calls #{super}" end end
  class B; def foo; "B" end end
  class C; def foo; "C" end end
  class D<B; include A; def foo; "D calls #{super}" end end
  class E<C; include A; def foo; "E calls #{super}" end end
  puts D.new.foo
  puts E.new.foo

displays:

  D calls A calls B
  E calls A calls C

But I don't know what that feature has to do with your problem though...

____________________________________________________________________
Mathieu Bouchard - tél:+1.514.383.3801 - http://artengine.ca/matju
Freelance Digital Arts Engineer, Montréal QC Canada

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: extend and super -- I cannot understand why this behavior
  2005-10-10 23:26     ` Mathieu Bouchard
@ 2005-10-10 23:59       ` TRANS
  2005-10-11 13:17         ` Austin Ziegler
  0 siblings, 1 reply; 7+ messages in thread
From: TRANS @ 2005-10-10 23:59 UTC (permalink / raw
  To: ruby-core

On 10/10/05, Mathieu Bouchard <matju@artengine.ca> wrote:
> On Tue, 11 Oct 2005, TRANS wrote:
> > Ah, I see. So it's the same @x in either method --I thinking super would
> > behave the same as calling Q::H.x. Is this why super is "hard bound" at
> > compile time.
>
> What do you mean by hard-bound at compile time? In a super-call, the self
> of the callee is always the self of the caller. However the
> classes/modules involved are not necessarily predetermined:
>
>   module A; def foo; "A calls #{super}" end end
>   class B; def foo; "B" end end
>   class C; def foo; "C" end end
>   class D<B; include A; def foo; "D calls #{super}" end end
>   class E<C; include A; def foo; "E calls #{super}" end end
>   puts D.new.foo
>   puts E.new.foo
>
> displays:
>
>   D calls A calls B
>   E calls A calls C

Yes, in modules it's not so, but for classes it is.

  class A
    def x ; 10 ; end
  end
  => nil

  class B < A
    def x ; super + 100 ; end
  end
  => nil

  class C < B
    def x ; super + 1000 ; end
  end
  => nil

  class D < C
    def x ; super + 10000 ; end
  end
  => nil

  d = D.new
  => #<D:0xb7d57e04>

  d.x
  => 11110

  B.instance_method(:x).bind(d).call
  => 110

Despite the method being bound lower down the inheritance chain, it's
super is still bound to the origianl call higher up.

> But I don't know what that feature has to do with your problem though...

Cause this works:

  module Q
     module H
       def x ; @x ||= [] ; end
     end
     extend H
   end

   class R
     include Q
     extend Q::H
     def self.x ; @x ||= [] ; ancestors[1].x + @x  ; end
     def self.x! ; @x ||= [] ; end
   end

Which I was mistakingly thinking that's what super would do.

T.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: extend and super -- I cannot understand why this behavior
  2005-10-10 23:59       ` TRANS
@ 2005-10-11 13:17         ` Austin Ziegler
  2005-10-11 15:50           ` TRANS
  0 siblings, 1 reply; 7+ messages in thread
From: Austin Ziegler @ 2005-10-11 13:17 UTC (permalink / raw
  To: ruby-core

On 10/10/05, TRANS <transfire@gmail.com> wrote:
>   module Q
>      module H
>        def x ; @x ||= [] ; end
>      end
>      extend H
>    end
>
>    class R
>      include Q
>      extend Q::H
>      def self.x ; @x ||= [] ; ancestors[1].x + @x  ; end
>      def self.x! ; @x ||= [] ; end
>    end
>
> Which I was mistakingly thinking that's what super would do.

Why? Instance variables are in the class, not the module. So, when
Q::H refers to @x, it's referring to @x in the class that has included
or extended itself with Q::H.

-austin
--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: extend and super -- I cannot understand why this behavior
  2005-10-11 13:17         ` Austin Ziegler
@ 2005-10-11 15:50           ` TRANS
  0 siblings, 0 replies; 7+ messages in thread
From: TRANS @ 2005-10-11 15:50 UTC (permalink / raw
  To: ruby-core

On 10/11/05, Austin Ziegler <halostatue@gmail.com> wrote:

> Why? Instance variables are in the class, not the module. So, when
> Q::H refers to @x, it's referring to @x in the class that has included
> or extended itself with Q::H.

No good reason. Just got it twisted a bit in my mind when I was
attempting to create an improvement on ClassMethods.

Thanks,
T.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-10-11 15:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-10-10 19:52 extend and super -- I cannot understand why this behavior TRANS
2005-10-10 20:28 ` Mathieu Bouchard
2005-10-10 21:01   ` TRANS
2005-10-10 23:26     ` Mathieu Bouchard
2005-10-10 23:59       ` TRANS
2005-10-11 13:17         ` Austin Ziegler
2005-10-11 15:50           ` TRANS

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).