Custom method_missing doesn't trap super call

When I run the code below it produces the following output:

ruby missing_super.rb
In method_missing: baz
In MyClass.foobar
missing_super.rb:10:in foobar': super: no superclass methodfoobar’
(NameError)
from missing_super.rb:16

The non existent call to ‘t.baz()’ is trapped ok. But in MyClass.foobar()
the ‘super’ call gives a ‘no superclass method’ error rather than calling
Base.method_missing as I expected. Does anyone know if there a way of
trapping the super call?

class Base
def method_missing(method, *args)
puts "In method_missing: " + method.id2name
end
end

class MyClass < Base
def foobar()
puts "In MyClass.foobar"
super
end
end

t = MyClass.new
t.baz()
t.foobar()

Thanks for any help - I’ve just started with ruby - the syntax is really
nice and ‘tidy’ (ie un-C++ like :slight_smile: ) The only other thing I got stuck with
is how to remove an item from a hash via C.

– Richard

Hi,

The non existent call to ‘t.baz()’ is trapped ok. But in MyClass.foobar()
the ‘super’ call gives a ‘no superclass method’ error rather than calling
Base.method_missing as I expected. Does anyone know if there a way of
trapping the super call?

I feel like it’s a bad idea in general, but without any particular
reason. Let me think.

Thanks for any help - I’ve just started with ruby - the syntax is really
nice and ‘tidy’ (ie un-C++ like :slight_smile: ) The only other thing I got stuck with
is how to remove an item from a hash via C.

Use rb_hash_delete(hash, key);

						matz.
···

In message “Custom method_missing doesn’t trap super call” on 03/07/15, Richard Dale Richard_Dale@tipitina.demon.co.uk writes:

Yukihiro Matsumoto wrote:

Hi,

The non existent call to ‘t.baz()’ is trapped ok. But in MyClass.foobar()
the ‘super’ call gives a ‘no superclass method’ error rather than calling
Base.method_missing as I expected. Does anyone know if there a way of
trapping the super call?

I feel like it’s a bad idea in general, but without any particular
reason. Let me think.
Perhaps I should explain more what about I was trying to do - I didn’t want
to over complicate the problem description. I’m working on bindings to a
C++ gui library Qt, and use a C method_missing (in the C ‘Qt’ extension) to
forward the calls to the C++ methods from ruby. When virtual methods are
overriden in ruby, like QPushButton::paintEvent(), it uses rb_respond_to()
to see whether or not there is a ruby version of the method.

if (rb_respond_to(obj, rb_intern(methodName)) == 0) {
return false;
}

Otherwise if rb_respond_to() returns true, call ruby methods like
‘paintEvent’ below via rb_funcall2()

Then the call to ‘super’ needs to correctly invoke the C++ paintEvent()
method via the method_missing trap.

require ‘Qt’;

class MyClass < Qt::PushButton
def paintEvent(event)
printf(“In paintEvent…\n”)
super
end
end

a = Qt::Application.new(ARGV, ref nil);
hello = MyClass.new(“Hello World”, nil)
hello.resize(100, 30)
a.setMainWidget(hello)
hello.show()
a.exec()

Thanks for any help - I’ve just started with ruby - the syntax is really
nice and ‘tidy’ (ie un-C++ like :slight_smile: ) The only other thing I got stuck
with is how to remove an item from a hash via C.

Use rb_hash_delete(hash, key);
Ah… thanks thats ideal - I couldn’t find it in intern.h with the other
rb_hash functions, so I assumed it didn’t exist.

– Richard

···

In message “Custom method_missing doesn’t trap super call” > on 03/07/15, Richard Dale Richard_Dale@tipitina.demon.co.uk writes:

I understand what you want. But why use method_missing? If Qt has
finite set of methods, why don’t you define wrapper methods just as
Swig does?

This does not mean I’m refusing “method_missing for super”. I’m still
thinking.

						matz.
···

In message “Re: Custom method_missing doesn’t trap super call” on 03/07/15, Richard Dale Richard_Dale@tipitina.demon.co.uk writes:

Perhaps I should explain more what about I was trying to do - I didn’t want
to over complicate the problem description. I’m working on bindings to a
C++ gui library Qt, and use a C method_missing (in the C ‘Qt’ extension) to
forward the calls to the C++ methods from ruby. When virtual methods are
overriden in ruby, like QPushButton::paintEvent(), it uses rb_respond_to()
to see whether or not there is a ruby version of the method.

Yukihiro Matsumoto wrote:

Perhaps I should explain more what about I was trying to do - I didn’t want
to over complicate the problem description. I’m working on bindings to a
C++ gui library Qt, and use a C method_missing (in the C ‘Qt’ extension) to
forward the calls to the C++ methods from ruby. When virtual methods are
overriden in ruby, like QPushButton::paintEvent(), it uses rb_respond_to()
to see whether or not there is a ruby version of the method.

I understand what you want. But why use method_missing? If Qt has
finite set of methods, why don’t you define wrapper methods just as
Swig does?

This does not mean I’m refusing “method_missing for super”. I’m still
thinking.

  					matz.

I just had occasion to invent a solution for this in something I was
working on. Unfortunately, I wasn’t paying enough attention to this
thread beforehand, so I don’t know if someone has already recommended a
solution similar to mine–or even worse, recommended against it. :slight_smile:

My goal is to wrap the instance variable @obj so that, from the outside
of the Wrapper object, it responds exactly like the @obj would.

---------8<---------
class Wrapper

# don't respond to any inherited public methods
(methods - ['__id__', '__send__']).each do |meth|
   begin
      undef_method meth.intern
   rescue
      # ignore
   end
end

def initialize(conn, klass, id)
   @obj = ...
end

def method_missing(meth, *args)
   @obj.__send__(meth, *args)
end

---------8<---------

Comments?

···

In message “Re: Custom method_missing doesn’t trap super call” > on 03/07/15, Richard Dale Richard_Dale@tipitina.demon.co.uk writes:

Yukihiro Matsumoto wrote:

Perhaps I should explain more what about I was trying to do - I didn’t
want to over complicate the problem description. I’m working on bindings
to a C++ gui library Qt, and use a C method_missing (in the C ‘Qt’
extension) to forward the calls to the C++ methods from ruby. When
virtual methods are overriden in ruby, like QPushButton::paintEvent(), it
uses rb_respond_to() to see whether or not there is a ruby version of the
method.

I understand what you want. But why use method_missing? If Qt has
finite set of methods, why don’t you define wrapper methods just as
Swig does?
The code is based on the PerlQt bindings project which use perl autoloading
to do what I want to do with method_missing in ruby. The clever thing about
it that it allows you to write adaptors for a language independent backend
library called ‘Smoke’, with very little code. I’ve just started with the
perlguts code in the perl adaptor and hacked it to work the same way in
ruby. It already does quite a lot - the paintEvent example works apart from
the ‘super’ call - everything else is fine.

With the Swig approach you can’t call protected methods, you can’t override
virtual methods, and it doesn’t have a mechanism for dealing with method
overloading very well (particularly important with constructors). With
Smoke I get all that for free, and I don’t even need to parse any
headers/write interfaces etc.

This does not mean I’m refusing “method_missing for super”. I’m still
thinking.
OK no problem - but from the method names, maybe the perl autoload feature
was intended to load native methods on demand, but perhaps method_missing
was aimed at custom error trapping?

– Richard

···

In message “Re: Custom method_missing doesn’t trap super call” > on 03/07/15, Richard Dale Richard_Dale@tipitina.demon.co.uk writes:

I’m interested in any (i.e., arbitrary) extensions of Ruby’s character
encoding.

As far as I can tell, the String class stops at integer value 255. For
example:

irb:> string << 256
TypeError: cannot convert Fixnum into String
from (irb):18:in `<<'
from (irb):18

Is there any way to extend the character encoding within Ruby? Has
anyone extended Ruby’s character encoding using C or C++?

I would like to be able to do

string << 3004

and have a character appended to the string. I actually don’t care what
the character is, so long as it is unique.

FYI, my motivation for this question is because I want to map a large
number of objects to characters. I want to do this mapping to
characters rather than other objects because I want to use
character-based tools to perform analysis (such as Levenstein distance).

Thank you in advance for any help.

Regards,

Mark

···

from :0

My goal is to wrap the instance variable @obj so that, from the outside
of the Wrapper object, it responds exactly like the @obj would.

<< snip code >>

Comments?

It looks like you have reinvented the delegator pattern:
http://www.rubycentral.com/book/lib_patterns.html

Cheers,

Brian.

···

On Tue, Jul 15, 2003 at 03:01:10PM +0900, Jim Cain wrote:

Richard Dale wrote:

With the Swig approach you can’t call protected methods, you can’t override
virtual methods, and it doesn’t have a mechanism for dealing with method
overloading very well (particularly important with constructors). With
Smoke I get all that for free, and I don’t even need to parse any
headers/write interfaces etc.

For the record, SWIG 1.3.20 will provide support for overriding C++
virtual methods with Ruby instance methods; this code is already
available in the SWIG development CVS. Method overloading has been
available since SWIG version 1.3.14 or so, and works very well for
constructors as well as regular functions.

On a side note, I hadn’t heard about Smoke, and so I was doing some
quick googling for it. It sounds pretty cool. Am I basically correct
that it is a Qt (or maybe Qt+KDE) specific library that can be used to
build different language bindings to Qt? In other words, it is not a
general purpose “wrapper” generator like SWIG?

Hi,

···

In message “Re: Custom method_missing doesn’t trap super call” on 03/07/15, Richard Dale Richard_Dale@tipitina.demon.co.uk writes:

This does not mean I’m refusing “method_missing for super”. I’m still
thinking.
OK no problem - but from the method names, maybe the perl autoload feature
was intended to load native methods on demand, but perhaps method_missing
was aimed at custom error trapping?

I guess so.

But I just found out the very important fact, that the original
script you’ve shown worked without any error for both 1.6.8 and 1.8.0
preview. What version were you using?

						matz.

Hi,

···

In message “Ruby Character Encoding” on 03/07/15, Mark Wilson mwilson13@cox.net writes:

I’m interested in any (i.e., arbitrary) extensions of Ruby’s character
encoding.

You need to wait for 1.9, it’s in ToDo, among others, such as
generational GC.

						matz.

Brian Candler wrote:

···

On Tue, Jul 15, 2003 at 03:01:10PM +0900, Jim Cain wrote:

My goal is to wrap the instance variable @obj so that, from the outside
of the Wrapper object, it responds exactly like the @obj would.

<< snip code >>

Comments?

It looks like you have reinvented the delegator pattern:
http://www.rubycentral.com/book/lib_patterns.html

Cheers,

Brian.

Well crap, wouldn’t you know… I guess I need to do some more exploring
through Ruby resources.

Yukihiro Matsumoto wrote:

Hi,

This does not mean I’m refusing “method_missing for super”. I’m still
thinking.
OK no problem - but from the method names, maybe the perl autoload
feature was intended to load native methods on demand, but perhaps
method_missing was aimed at custom error trapping?

I guess so.

But I just found out the very important fact, that the original
script you’ve shown worked without any error for both 1.6.8 and 1.8.0
preview. What version were you using?
Oh, that’s interesting. I have:

baldhead duke 1103% ruby -v
ruby 1.6.7 (2002-03-19) [ppc-linux]

– Richard

···

In message “Re: Custom method_missing doesn’t trap super call” > on 03/07/15, Richard Dale Richard_Dale@tipitina.demon.co.uk writes:

Lyle Johnson wrote:

I’ve just sent you a private mail reply, but I’ll post it to the newsgroup
apart from the qtruby attachment - I assume 32k isn’t popular there…

Richard Dale wrote:

With the Swig approach you can’t call protected methods, you can’t
override virtual methods, and it doesn’t have a mechanism for dealing
with method overloading very well (particularly important with
constructors). With Smoke I get all that for free, and I don’t even need
to parse any headers/write interfaces etc.

For the record, SWIG 1.3.20 will provide support for overriding C++
virtual methods with Ruby instance methods; this code is already
available in the SWIG development CVS. Method overloading has been
available since SWIG version 1.3.14 or so, and works very well for
constructors as well as regular functions.
Oops, I’m sorry if I sounded a bit rude about SWIG, it sounds as though it’s
making good progress. I started generating Qt/KDE bindings with a version of
kdoc (a tool written in perl which parsed headers and produced html), called
‘kalyptus’. I added a kalyptus ruby generation option based on producing the
same code as SWIG about a year and a half ago, but got stuck with the method
overloading. I think Nobuyuki Horie had to do a lot of hand fixing for the
RubyQt bindings for Qt2.

Then Ashley Winters came along with the idea of SMOKE. That stands for
‘Scripting Meta Object Kompiler Engine’, it works like the moc tool in Qt
which adds some runtime dynamism to static C++ applications via a
preprocessor, but just for slots, signals and properties. SMOKE does the
something similar, but for the entire api.

On a side note, I hadn’t heard about Smoke, and so I was doing some
quick googling for it. It sounds pretty cool. Am I basically correct
that it is a Qt (or maybe Qt+KDE) specific library that can be used to
build different language bindings to Qt? In other words, it is not a
general purpose “wrapper” generator like SWIG?
The SMOKE bindings are currently generated with kalyptus by parsing headers.
But Ashley is working on a tool which parses the compiled translation unit
for a library, which he thinks will be more precise. I don’t think anyone on
the kdebindings list is really thinking about general purpose tools like
SWIG, and Qt/KDE has it’s own specific problems like how to deal with
slots/signals. But I can’t see why the idea couldn’t be used with other
large C++ libraries. I think you’ll always need to write marshalling code -
typemaps in SWIG or the marshalling in handlers.cpp in the attached qtruby
code. And I don’t know whether or not the bindings lib has a dynamic runtime
would be noticed by a user. The advantage for KDE of a common shared library
like libsmokeqt.so is that it can be loaded at start up by the kdeinit
process, and then the language extensions are very small and quick to load.

– Richard

Yukihiro Matsumoto wrote:

But I just found out the very important fact, that the original
script you’ve shown worked without any error for both 1.6.8 and 1.8.0
preview. What version were you using?
Oh, that’s interesting. I have:

baldhead duke 1103% ruby -v
ruby 1.6.7 (2002-03-19) [ppc-linux]
I’ve just tried 1.6.8, a more recent stable version of 1.6.8 and 1.8 preview
1 on Yellow Dog Linux 3.0 and none of them trap the super call. I tried
1.6.7 on Mac OS X and that was the same. So is it something to do with
PowerPC I wonder? This is the version info from gcc if that’s relevant:

Reading specs from /usr/lib/gcc-lib/ppc-yellowdog-linux/3.2.2/specs
Configured with: …/configure --prefix=/usr --mandir=/usr/share/man
–infodir=/usr/share/info --enable-shared --enable-threads=posix
–disable-checking --with-system-zlib --enable-__cxa_atexit
–host=ppc-yellowdog-linux
Thread model: posix
gcc version 3.2.2 20030217 (Yellow Dog Linux 3.0 3.2.2-2a)

– Richard

It doesn’t work for me, with ruby 1.6.8 on Linux/i386 (Mandrake) and
Linux/PPC (gentoo).

Are we missing something?

Guillaume.

gus@mac gus $ ruby

class Base
def method_missing(method, *args)
puts "In method_missing: " + method.id2name
end
end

class MyClass < Base
def foobar()
puts “In MyClass.foobar”
super
end
end

t = MyClass.new
t.baz()
t.foobar()
In method_missing: baz
In MyClass.foobar
-:11:in foobar': super: no superclass method foobar’ (NameError)
from -:17

···

On Tue, 2003-07-15 at 11:43, Yukihiro Matsumoto wrote:

Hi,

In message “Re: Custom method_missing doesn’t trap super call” > on 03/07/15, Richard Dale Richard_Dale@tipitina.demon.co.uk writes:

This does not mean I’m refusing “method_missing for super”. I’m still
thinking.
OK no problem - but from the method names, maybe the perl autoload feature
was intended to load native methods on demand, but perhaps method_missing
was aimed at custom error trapping?

I guess so.

But I just found out the very important fact, that the original
script you’ve shown worked without any error for both 1.6.8 and 1.8.0
preview. What version were you using?

  					matz.

Jim Cain wrote:

Brian Candler wrote:

My goal is to wrap the instance variable @obj so that, from the outside
of the Wrapper object, it responds exactly like the @obj would.

<< snip code >>

Comments?

It looks like you have reinvented the delegator pattern:
http://www.rubycentral.com/book/lib_patterns.html

Cheers,

Brian.

Well crap, wouldn’t you know… I guess I need to do some more exploring
through Ruby resources.
But thanks - I’ll experiment with the delegator suggestions - I think this
is a fun problem to try and solve somehow…

– Richard

···

On Tue, Jul 15, 2003 at 03:01:10PM +0900, Jim Cain wrote:

Meanwhile, there are modules in RAA to handle processing of Unicode,
and I have a mostly-complete 1.8 implementation of U8String on
top of those modules, which acts just like String except that it
is initialized from and prints out as UTF-8, and the unit for
etc. is Unicode characters rather than bytes.

-Mark

···

On Tue, Jul 15, 2003 at 04:21:40PM +0900, Yukihiro Matsumoto wrote:

I’m interested in any (i.e., arbitrary) extensions of Ruby’s character
encoding.

You need to wait for 1.9, it’s in ToDo, among others, such as
generational GC.

Hi,

···

In message “Re: Custom method_missing doesn’t trap super call” on 03/07/16, Richard Dale Richard_Dale@tipitina.demon.co.uk writes:

I’ve just tried 1.6.8, a more recent stable version of 1.6.8 and 1.8 preview
1 on Yellow Dog Linux 3.0 and none of them trap the super call. I tried
1.6.7 on Mac OS X and that was the same. So is it something to do with
PowerPC I wonder? This is the version info from gcc if that’s relevant:

Hmm, I don’t have any powerpc machine at hand. Let me see though.

						matz.

FWIW, I get the same results in 1.6.8 under FreeBSD on i386, so I don’t
think it’s platform-specific.

$ ruby -v x.rb
ruby 1.6.8 (2002-12-24) [i386-freebsd4.8]
In method_missing: baz
In MyClass.foobar
x.rb:10:in foobar': super: no superclass method foobar’ (NameError)
from x.rb:16

Brian.

···

On Wed, Jul 16, 2003 at 07:28:45AM +0900, Guillaume Marcais wrote:

It doesn’t work for me, with ruby 1.6.8 on Linux/i386 (Mandrake) and
Linux/PPC (gentoo).

Are we missing something?