Calling super methods

G

Grzegorz Dostatni

Is it possible to call a method of a superclass? Let's say I have a
instance of class Bar ( subclassed from Foo).
Both Foo and Bar define a function h()

I want to call a.h() and have it go the Foo's implementation if it,
without going through the super() call (ie. do it from outside the
object).

I have a code in python that does it:

class Foo:
def h(self):
print "Foo"

class Bar(Foo):
def h(self):
print "Bar"

b = Bar()

b.h()
Greg

"When ideas fail, words come in very handy."
- Goethe (1749-1832)
 
F

Florian Frank

class Foo:
def h(self):
print "Foo"

class Bar(Foo):
def h(self):
print "Bar"

b = Bar()

b.h()

class Foo
def h
puts "Foo"
end
end
class Bar < Foo
def h
puts "Bar"
end
end
b = Bar.new
Foo.instance_method:)h).bind(b).call # prints "Foo"
 
A

Andrew Johnson

Is it possible to call a method of a superclass? Let's say I have a
instance of class Bar ( subclassed from Foo).
Both Foo and Bar define a function h()

I want to call a.h() and have it go the Foo's implementation if it,
without going through the super() call (ie. do it from outside the
object).

I have a code in python that does it:

class Foo:
def h(self):
print "Foo"

class Bar(Foo):
def h(self):
print "Bar"

b = Bar()

b.h()

Perhaps there's a shorter call sequence, but ...

class Foo
def h; puts "foo";end
end
class Bar < Foo
def h; puts "bar";end
end

a = Bar.new
a.h #=> 'bar'

## hardcode the call-up class:
Foo.instance_method:)h).bind(a).call #=> 'foo'

## more dynamic call-up:
a.class.superclass.instance_method:)h).bind(a).call #=> 'foo'

## Or, add dynamic call_up to the Object class:
class Object
def supercede(meth)
self.class.superclass.instance_method(meth).bind(self).call
end
alias :call_up :supercede
end

a.h #=> 'bar'
a.supercede:)h) #=> 'foo'
a.call_up:)h) #=> 'foo'
__END__
 
F

Florian Frank

## Or, add dynamic call_up to the Object class:
class Object
def supercede(meth)
self.class.superclass.instance_method(meth).bind(self).call
end
alias :call_up :supercede
end

a.h #=> 'bar'
a.supercede:)h) #=> 'foo'
a.call_up:)h) #=> 'foo'
__END__

supercede is nice. To build on that:

[...]
class Object
def supercede(meth, n = 1)
klass = (1..n).inject(self.class) do |klass,|
klass.__send__ :superclass
end
klass.instance_method(meth).bind(self).call
end
end

a.h #=> 'bar'
a.supercede:)h) #=> 'foo'
a.supercede:)h, 2) #=> 'foobar'
 
M

MiG

Hello,

is there anyone betters in QTRuby or Korundum? Of course, there are
examples in the tgz, but I'm especially concerned about UIC - creating
widgets on the fly from XML.

I can run the example and create widget but don't know what to do
further. For instance I have created some buttons in Designer and now
don't know how to access them in Ruby.

One example is better than 10GB manual :) so I please anyone to write
preferably complete tutorial. I think ruby+kde/qt+xml could be VERY
powerful thing but people are lazy and want to learn everything as soon
as possible. Usually the best thing is not the winning :-(
If I'll tell my chief about Korundum, he would probably say that's
great but without manual we will prefer Java in the future.

Thank you!

MiG
 
A

Alexander Kellett

One example is better than 10GB manual :) so I please anyone to write
preferably complete tutorial. I think ruby+kde/qt+xml could be VERY
powerful thing but people are lazy and want to learn everything as soon
as possible. Usually the best thing is not the winning :-(
If I'll tell my chief about Korundum, he would probably say that's
great but without manual we will prefer Java in the future.

you make an excellent point.
i'm in a mood to improve my ability
to write english and to improve a few
little qt/ruby apps that i made. i'll
do a full tutorial on the development
of a given application. i've never
used qtdesigner either. so the tutorial
will be written from the point of a
newbie for a newbie :)

Alex
 
R

Richard Dale

Alexander said:
you make an excellent point.
i'm in a mood to improve my ability
to write english and to improve a few
little qt/ruby apps that i made. i'll
do a full tutorial on the development
of a given application. i've never
used qtdesigner either. so the tutorial
will be written from the point of a
newbie for a newbie :)
All I've done is take existing .ui files and make sure they can either be
compiled to ruby with the rbuic tool, or read in at runtime via
QUI::WidgetFactory.create. I'm a newbie too. So we really do need examples.
Is there a Qt Designer tutorial that could be translated from C++ to ruby
like I did with the cannon game tutorial? Thats a lot easier than starting
from scratch.

I find the Qt Designer UI takes a bit of getting used to. I was expecting to
be able to drag widgets off the palette like you can in Apple's Interface
Builder. Instead you click on the widget on the palette, and it puts the
cursor in 'place widget' mode. It took me about 10 mins to work out what
that was about. If you double click on the palette it gets stuck in 'place
widget' mode..

-- Richard
 
R

Richard Dale

Richard said:
All I've done is take existing .ui files and make sure they can either be
compiled to ruby with the rbuic tool, or read in at runtime via
QUI::WidgetFactory.create. I'm a newbie too. So we really do need
examples. Is there a Qt Designer tutorial that could be translated from
C++ to ruby like I did with the cannon game tutorial? Thats a lot easier
than starting from scratch.
Actually there is a Qt Designer tutorial:

http://doc.trolltech.com/3.3/designer-manual-3.html

It just needs expanding to show how you generate ruby code with rbuic, and
then build the complete app.
 
A

Alexander Kellett

All I've done is take existing .ui files and make sure they can either be
compiled to ruby with the rbuic tool, or read in at runtime via
QUI::WidgetFactory.create. I'm a newbie too. So we really do need examples.
Is there a Qt Designer tutorial that could be translated from C++ to ruby
like I did with the cannon game tutorial? Thats a lot easier than starting
from scratch.

i prefer widgetfactory.create
the entire rbuic / uic idea
disgusts me.

anyways. good point about previous
work :). i'll check.
I find the Qt Designer UI takes a bit of getting used to. I was expecting to
be able to drag widgets off the palette like you can in Apple's Interface
Builder. Instead you click on the widget on the palette, and it puts the
cursor in 'place widget' mode. It took me about 10 mins to work out what
that was about. If you double click on the palette it gets stuck in 'place
widget' mode.

i still want to rewrite qt designer :)
still hate it :p
Alex
 
H

Henrik Horneber

Richard said:
Alexander Kellett wrote:



All I've done is take existing .ui files and make sure they can either be
compiled to ruby with the rbuic tool, or read in at runtime via
QUI::WidgetFactory.create. I'm a newbie too. So we really do need examples.
Is there a Qt Designer tutorial that could be translated from C++ to ruby
like I did with the cannon game tutorial? Thats a lot easier than starting
from scratch.

I find the Qt Designer UI takes a bit of getting used to. I was expecting to
be able to drag widgets off the palette like you can in Apple's Interface
Builder. Instead you click on the widget on the palette, and it puts the
cursor in 'place widget' mode. It took me about 10 mins to work out what
that was about. If you double click on the palette it gets stuck in 'place
widget' mode..

-- Richard

I hope I am not pointing out the obvious, but the QtAssistant has a
section dedicated to using .uic files from code. In C++ of course. You
find it under the QtDesigner Manual, which seemed a little odd to me at
first, because it deals with using the Widgets from code.

Seems using the WidgetFactory is key.

Just in case you didn't know that. :)
 
A

Alexander Kellett


as is my normal opinion of tutorials...
this is just awful. *way* too long.
i want the *first page* of my
tutorial to already have something
pretty dang impressive on ones screen.
just hooks. people can fill in. don't
need to step by step. just teach ideas.
give them the possibility to make the
leaps by giving them the steps :)
i'll write something myself for certain :p
thanks for tutorial anyways.
nice to know that it exists :)
cheers,
Alex
 
F

Florian Gross

Grzegorz said:
Is it possible to call a method of a superclass? Let's say I have a
instance of class Bar ( subclassed from Foo).
Both Foo and Bar define a function h()

I want to call a.h() and have it go the Foo's implementation if it,
without going through the super() call (ie. do it from outside the
object).

Ah, this has been asked before. I attached my implementation. With this
one you don't need to pass the method name.

Regards,
Florian Gross


# Like super this calls methods from the inheritance chain which this
# method is replacing. However this version jumps over one or more
# methods in the inheritance chain. It is used like this:
#
# class X
# def it; p "X#it"; end
# end
#
# class Y < X
# def it; p "Y#it"; end
# end
#
# class Z < Y
# def it
# p "Z#it"
# superjump
# end
# end
#
# Z.new.it # outputs "X#it", "Z#it"
#
# Be careful. This method can't automatically pass the arguments of the
# caller like super does. You'll have to manually supply them.
def superjump(gap = 0, *args, &block)
method_name = caller[0][/in `(.*?)'/, 1].intern
klass = self.class.ancestors[1 + gap]
klass.instance_method(method_name).bind(self).call(*args, &block)
end
 
R

Robert Klemme

Grzegorz Dostatni said:
Is it possible to call a method of a superclass? Let's say I have a
instance of class Bar ( subclassed from Foo).
Both Foo and Bar define a function h()

I want to call a.h() and have it go the Foo's implementation if it,
without going through the super() call (ie. do it from outside the
object).

May I ask why you need that? IMHO it's no good OO design practice to
explicitely select the method to invoke. Methods are overridden on
purpose, so if you have a sub class instance the sub class method should
be invoked. If you need special functionality that is present in the base
class method, then this is typically an indication that it should be
factored out of that method into a separate method.

Regards

robert
 
M

MiG

yes,

there is *VERY* simple example of QYWidgetFactory only creating window
form .ui file:

QUI::WidgetFactory.loadImages ARGV[0]
w = QUI::WidgetFactory.create ARGV[1]
w.show()

But I don't know how to access it's components, etc.

Good documentation with many examples is needed. It's the best way how
to write KDE apps using Ruby and spread Ruby itself. Only few people
will use it else. I'm not C++ programmer, don't understand C++ well
and therefore can't get inspired from C++ code fastly. I think people
don't want to mess about tons of code, they need working examples. That
is why lack of documentation got rid of many good projects.


Jan Molic
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top