Strangeness with calling super class methods from JRuby

Hi all

I’m encountering this rather strange issue.

I have this setup going.

JavaClass.java
public class JavaClass
{
public void pushString() { … }
}

RubyClass.rb
class RubyClass < JavaClass
def pushString
super.pushString # error! (NoMethodError) undefined method
`pushString’
end

end

=====
However, if I have this
JavaClass.java
public class JavaClass implements Serializable
{
public void pushString() {…}

public void writeReplace() {… } // method from Serializable
}

RubyClass.rb
class RubyClass < JavaClass
java_implements ‘Serializable’
java_signature ‘public Object writeReplace()’
def writeReplace
super.pushString #works
return…
end

end

=====
BUT this doesn’t:
Interface.java
public interface Interface
{
public void test();
public Object test2();
}

JavaClass.java
public class JavaClass implements Serializable, Interface
{
public void pushString() {…}

public void writeReplace() {… } // method from Serializable

public void test() { … }

public Object test2() { return “S”; }
}

RubyClass.rb
class RubyClass < JavaClass
java_implements ‘Serializable, Interface’
java_signature ‘public Object writeReplace()’
def writeReplace
super.pushString #works
return…
end

java_signature ‘public void test()’
def test
super.pushString # no method error again from nil class
end

java_signature ‘public void test2()’
def test2
super.pushString # no method error , but this time it says
(NoMethodError) undefined method `pushString’ for “S”:String
end

end

=====
Why is this so…?
I’m curious as to why I can call super methods from Serializable’s
interface methods, but doing the same for my own interface doesn’t work.
Also, the last error is even stranger, why would it say “S”:String,
which is the return for my super class’s method?

What’s the best practice for calling super methods? java_send or super?

Thanks!

Regards
Angel

From Programming in Ruby by Dave T. and Andy H.

“In example 2, we create a subclass of class Song, overriding both the
initialize
and to s methods. In both of the new methods we use the super keyword to
invoke
the equivalent method in our parent class. In Ruby, super is not a
reference to a parent
class; instead it is an executable statement that reinvokes the current
method, skipping
any definition in the class of the current object.”

Cheers,
Luís Landeiro R.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs