Call functions of superclass

A

ara.t.howard

much better, imho, and used all over my own code is

def singleton_class &b
sc = class << self; self; end
b ? sc.module_eval &b : sc
end

obj = Object.new

obj.singleton_class{ def foo() 42 end }

2 cts.

-a
 
J

Justin Bailey

+100 - please give us singleton_class!
much better, imho, and used all over my own code is

def singleton_class &b
sc = class << self; self; end
b ? sc.module_eval &b : sc
end

obj = Object.new

+1 to this implementation.
 
D

dblack

Hi --

Exactly! That's the point I was getting at. It'd be nice if this would
work. "self" points to the object who's method the interpreter is in, so
playing by that same game, "super" should point to the superclass of the
object who's method the interpreter is in.

It depends what you mean by "should" :) I think the current behavior
of super (looking for the next same-named method in a higher module or
class) is very useful, and should not be eliminated. So if a keyword
is introduced to be a synonym for self.class.superclass, it should
probably be something else.


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
N

Nathan Smith

Hi --



It depends what you mean by "should" :) I think the current behavior
of super (looking for the next same-named method in a higher module or
class) is very useful, and should not be eliminated. So if a keyword
is introduced to be a synonym for self.class.superclass, it should
probably be something else.

If I'm thinking correctly, self.class.superclass will point to the
metaclass of the superclass of self. This isn't what is wanted when
calling super.someMethod (that would be like doing
super.class.someMethod, which is different). I recognize Ruby is different
from other langauges, but many other languages (even less OO capable
languages such as Java and C++) have the use of the "super.someMethod"
functionality.

Nate
 
J

Julian 'Julik' Tarkhanov

He has redefined the 'another' method in the child class. But for
some
reason, he needs the 'another' method in the parent class instead.

I am not saying I _need_ to do that, I was curious if it's at all
possible. It is in PHP for instance (in Python too AFAIK).
 
D

dblack

Hi --

If I'm thinking correctly, self.class.superclass will point to the
metaclass of the superclass of self.

No; it will point to the superclass of the class of self :)

class C
end
class D < C
end
D.new.class.superclass # C
This isn't what is wanted when calling super.someMethod (that would
be like doing super.class.someMethod, which is different). I
recognize Ruby is different from other langauges, but many other
languages (even less OO capable languages such as Java and C++) have
the use of the "super.someMethod" functionality.

Well... it's probably good that Ruby isn't a superset of those less OO
capable languages :) But I'm getting confused by the use of an
existing keyword to describe a new concept. Also, it's not clear what
the keyword would actually produce. You're sending the someMethod
message to it, but from what I understand you don't really mean it to
respond to that method, but rather to re-send the message to the
current self, using the constraint that the currently visible version
of the method be skipped.

I wondering whether perhaps something other than message-sending
semantics would be better, since that's an awful lot to hide behind
the dot. Maybe a block-wise evaluation of some kind?


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
M

Matthew Johnson

# Sigh -- Matz, *please* can we have this? :)
+100 - please give us singleton_class!

I concur as well and have been using this myself.
+1 to this implementation.

or the alternative:

def singleton_eval &b
singleton_class.module_eval &b
end
 
B

Bart Braem

Farrel said:
class Parent
def useful(parameters)
#Do stuff
end
end
class Child < Parent
def useful(parameters)
super(parameters)
# Do more stuff..
end
end

That's what I was looking for, altough it's a strange use of the super
keyword.
(James Edward Gray II: you were right about the Child < Parent part, I
forgot to include it in my example.)
Another question: I am a bit confused by other responses in this thread. Say
I use the following code:

class Parent
def somemethod
end
def overrideme(param)
end
end

class Child < Parent
def overrideme(param)
# do stuff
super(param)
somemethod
end
end

The somemethod call in Child#overrideme works. Some posts in this thread
seem to say this impossible?

Bart (still learning ruby and starting to love it!)
 
T

ts

M> A more correct conclusion would have been that Ruby's 'super' should
M> be regarded more as a method call rather than as a pseudo-variable

yes, super can be seen as a method call.
 
N

Nathan Smith

Hello,

No; it will point to the superclass of the class of self :)

class C
end
class D < C
end
D.new.class.superclass # C


I think we are using different terminology. Take a look at this code:

class A
def A.hi
puts "A.hi"
end

def hi
puts "hi"
end
end

class B < A
def hi
self.class.superclass.hi
end
end

b = B.new
b.hi


This will output

A.hi


In "Programming Ruby", they define the "metaclass" as the object which
contains the class-wide objects of the class. IE, the metaclass of A would
contain A.hi, and not A#hi (I could be getting the notation of A.hi and
A#hi backwards, or completely wrong -- A.hi is the class method, and A#hi
is the instance method.)

Therefore, in B#hi, self.class.superclass does not refer to the superclass
of B -- rather it refers to the metaclass of the super of B. If it pointed
to the superclass of B, then calling

self.class.superclass.hi

would print "hi", instead of "A.hi"

Well... it's probably good that Ruby isn't a superset of those less OO
capable languages :)

I'm not suggesting that Ruby be a superset of those languages (and I'm
glad it isn't) -- I'm suggesting that the other languages got the
terminology right in using both the "self" and the "super" keywords in the
same context.
But I'm getting confused by the use of an existing keyword to describe a
new concept. Also, it's not clear what the keyword would actually
produce. You're sending the someMethod message to it, but from what I
understand you don't really mean it to respond to that method, but
rather to re-send the message to the current self, using the constraint
that the currently visible version of the method be skipped.

I do want the message to be responded to. Calling super.someMethod would
pass the someMethod message to the super object. The message is not being
re-sent to any object, nor is it being routed through any object. It is
being sent directly to the super object.

"super" would either point to the superclass of self (the instance
version), or (in the case of self.class.super), would refer to the
metaclass of the superclass of self.

Perhaps Matz wants to keep the metaclass completely hidden from the
programmer (indeed, it is hinted so in Programming Ruby), and I can see
the merits in doing so. This still requires more work for the programmer
to be able to perform what was stated earlier in this thread.

Nate
 
T

ts

N> Therefore, in B#hi, self.class.superclass does not refer to the superclass
N> of B -- rather it refers to the metaclass of the super of B. If it pointed

no, not really. self.class.superclass make reference to the superclass of
B

N> to the superclass of B, then calling

N> self.class.superclass.hi

N> would print "hi", instead of "A.hi"

no, if self.class.superclass == A then self.class.superclass.hi is the
same than A.hi, and ruby will print "A.hi"


Guy Decoux
 
N

Nathan Smith

N> Therefore, in B#hi, self.class.superclass does not refer to the superclass
N> of B -- rather it refers to the metaclass of the super of B. If it pointed

no, not really. self.class.superclass make reference to the superclass of
B

It refers to the metaclass of the superclass of B -- see Programming Ruby
pp. 380-381.
N> to the superclass of B, then calling

N> self.class.superclass.hi

N> would print "hi", instead of "A.hi"

no, if self.class.superclass == A then self.class.superclass.hi is the
same than A.hi, and ruby will print "A.hi"

Inside of a class instance method, self.class returns a reference to the
metaclass of the class. Calling superclass on the metaclass returns
another metaclass, and you're right -- it will print A.hi (as I said =])

Nate
 
T

ts

N> Inside of a class instance method, self.class returns a reference to the
N> metaclass of the class. Calling superclass on the metaclass returns
N> another metaclass, and you're right -- it will print A.hi (as I said =])

re read your example

N> class B < A
N> def hi
N> self.class.superclass.hi
N> end
N> end

moulon% cat b.rb
#!/usr/bin/ruby
class A
end

class B < A
def hi
p self.class
p class << self.class; self end
end
end

B.new.hi
moulon%

moulon% ./b.rb
B
#<Class:B>
moulon%


You really think that self.class, i.e. B, make reference to the singleton
class of B ?


Guy Decoux
 
N

Nathan Smith

N> Inside of a class instance method, self.class returns a reference to the
N> metaclass of the class. Calling superclass on the metaclass returns
N> another metaclass, and you're right -- it will print A.hi (as I said =])

Guy Decoux
You really think that self.class, i.e. B, make reference to the singleton
class of B ?

Re-read what I said:
N> Inside of a class instance method, self.class returns a reference to
the metaclass of the class.

metaclass != singleton class

Nate
 
T

ts

N> metaclass != singleton class

Well, it's well known : metaclass exist in Smalltalk, singleton class in
ruby. Nothing new :)


Guy Decoux
 
N

Nathan Smith

N> metaclass != singleton class

Well, it's well known : metaclass exist in Smalltalk, singleton class in
ruby. Nothing new :)

That's great. Thanks for the info ;-) I'm referring to metaclass as stated
in Programming Ruby, not smalltalk.

Nate
 
T

ts

N> That's great. Thanks for the info ;-) I'm referring to metaclass as stated
N> in Programming Ruby, not smalltalk.

Re read carefully "Programming Ruby", it was written by someone which know
ruby better than you ...


Guy Decoux
 
N

Nathan Smith

N> That's great. Thanks for the info ;-) I'm referring to metaclass as stated
N> in Programming Ruby, not smalltalk.

Re read carefully "Programming Ruby", it was written by someone which know
ruby better than you ...

Rather than making rash statements such as that, it'd be nice of you to
successfully debunk my arguments. Keep trying, I have faith in you.

Nate
 
D

dblack

Hi --

Hello,




I think we are using different terminology. Take a look at this code:

class A
def A.hi
puts "A.hi"
end

def hi
puts "hi"
end
end

class B < A
def hi
self.class.superclass.hi
end
end

b = B.new
b.hi


This will output

A.hi

Yes, because B.new.class.superclass is A, and A responds to "hi". The
method A.hi is defined in A's singleton class (or "metaclass") -- but
the object to which you are sending the message "hi" is A, not A's
singleton class.
In "Programming Ruby", they define the "metaclass" as the object which
contains the class-wide objects of the class. IE, the metaclass of A would
contain A.hi, and not A#hi (I could be getting the notation of A.hi and
A#hi backwards, or completely wrong -- A.hi is the class method, and A#hi
is the instance method.)

Therefore, in B#hi, self.class.superclass does not refer to the superclass
of B -- rather it refers to the metaclass of the super of B. If it pointed
to the superclass of B, then calling

self.class.superclass.hi

would print "hi", instead of "A.hi"

No, because A is the superclass of the class of "self" in your
example. A is an object in its own right; it responds to "hi".

I think you're adding an extra level of reference or indirection or
something. Look at this:

irb(main):001:0> class C; object_id; end
=> 1615634
irb(main):002:0> C.new.class.object_id
=> 1615634
irb(main):003:0> C.new.class.superclass.object_id
=> 1683084
irb(main):004:0> C.new.class.superclass
=> Object
irb(main):005:0> Object.object_id
=> 1683084
irb(main):006:0> class << C; object_id; end
=> 1615554

As you can see, the singleton class of C is not the same object as
either C or the class of C.

("Singleton class" is the most general term; the Pickaxe uses
"metaclass" to mean singleton class of a Class object. I tend to just
use "singleton".)


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
N

Nathan Smith

Hi --



Yes, because B.new.class.superclass is A, and A responds to "hi". The
method A.hi is defined in A's singleton class (or "metaclass") -- but
the object to which you are sending the message "hi" is A, not A's
singleton class.


No, because A is the superclass of the class of "self" in your
example. A is an object in its own right; it responds to "hi".

I think you're adding an extra level of reference or indirection or
something. Look at this:

irb(main):001:0> class C; object_id; end
=> 1615634
irb(main):002:0> C.new.class.object_id
=> 1615634
irb(main):003:0> C.new.class.superclass.object_id
=> 1683084
irb(main):004:0> C.new.class.superclass
=> Object
irb(main):005:0> Object.object_id
=> 1683084
irb(main):006:0> class << C; object_id; end
=> 1615554

As you can see, the singleton class of C is not the same object as
either C or the class of C.

("Singleton class" is the most general term; the Pickaxe uses
"metaclass" to mean singleton class of a Class object. I tend to just
use "singleton".)

I concede your point -- I was using singleton as the object-specific
class, while it is the opposite of that. Thanks for the clarification.
Maybe now my arguments will make more sense!

Nate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top