[rjb-users] Extending a Java class
rjb-users at rubyforge.org
rjb-users at rubyforge.org
Wed Nov 1 10:17:47 EST 2006
Hi Arton,
The delegation idea is nice. I gave it a try and it
worked. Now I have a more difficult question.
Here's a situation I'd like to get working. I have a
Java library. A class has a static method that takes
an argument of type Foo:
public class Foo
{
//...
public void bar()
{
//...
}
}
and....
package my.lib;
public class Util
{
//...
public static void doSomething(Foo foo)
{
// use F
}
}
In Ruby, I would like to create a pure Ruby class that
works like the Java class Foo:
class FakeFoo
#...
def bar
#implement
end
end
I'd like to be able to do this:
require 'rjb'
Util=Rjb::import 'my.lib.Util'
ff=FakeFoo.new
Util.doSomething(ff)
When I try this now, I get a method_missing error. But
what is the method that's missing? What can I do to
FakeFoo to make it work with the Util.doSomething
method?
cheers,
Rich
--- arton <arton at e07.itscom.net> wrote:
> Hi Rich,
>
> At this time, the imported class is not a really
> Ruby class in Rjb.
> If you think it's better to map imported class as
> Ruby's class, I positively
> plan to implement the feature.
> So, at this time, a workaround is to use delegation
> instead of
> inheritance.
> This sample code describes how to delegate method
> calls to the contained
> object.
>
> require 'rjb'
> require 'test/unit'
>
> class RubyVector
> Vector = Rjb::import('java.util.Vector')
> def initialize
> @vector = Vector.new
> end
> def test_vector
> 'it worked'
> end
> def method_missing(msg, *arg)
> @vector.send(msg, *arg)
> end
> end
>
> class VectorTest < Test::Unit::TestCase
>
> def setup
> @r = RubyVector.new
> end
>
> def test_adding_method()
> assert_equal('it worked', @r.test_vector)
> end
>
> def test_add()
> @r.add(1)
> @r.add(2)
> @r.add(3)
> assert(@r.contains(1))
> assert(!@r.contains(8))
> end
>
> def test_get()
> @r.add("ab")
> @r.add("cd")
> @r.add("ef")
> assert_equal("ab", @r.get(0).toString)
> assert_equal("cd", @r.get(1).toString)
> assert_equal("ef", @r.get(2).toString)
> end
>
> def test_iterator()
> @r.add("ab")
> @r.add("cd")
> @r.add("ef")
> i = @r.iterator
> assert(i.hasNext)
> assert_equal("ab", i.next.toString)
> assert(i.hasNext)
> assert_equal("cd", i.next.toString)
> assert(i.hasNext)
> assert_equal("ef", i.next.toString)
> assert(!i.hasNext)
> end
> end
>
> Best reagrds
>
> On Mon, 30 Oct 2006 22:07:02 -0800 (PST)
> richard apodaca <rich_apodaca at yahoo.com>¤µ¤ó wrote:
>
> > Hi Arton,
> >
> > I was wondering how to extend a Java class with
> RJB. I
> > saw in the documentation how to bind a Java
> interface
> > to a Ruby class, but can I extend a Java class
> with
> > Ruby? Here's an example of what I've tried:
> >
> > require 'rubygems'
> > require_gem 'rjb'
> > require 'rjb'
> >
> > Vector=Rjb::import 'java.util.Vector'
> >
> > class Test < Vector.class
> > def initialize
> > end
> > def test_vector
> > puts "it worked"
> > end
> > end
> >
> > t=Test.new # => TypeError: allocator undefined for
> > Test
> >
> > I get a TypeError with the above code. If I try to
> > extend directly form Vector rather than
> Vector.class,
> > I get:
> >
> > TypeError: superclass must be a Class
> >
> > Any ideas?
> >
> > thanks,
> > Rich
> >
> >
> > ____________________________
> > Richard Apodaca
> > Blog: http://depth-first.com
> >
> >
> >
> >
>
____________________________________________________________________________________
> > Want to start your own business? Learn how on
> Yahoo! Small Business
> > (http://smallbusiness.yahoo.com)
> >
>
> --
> arton
>
>
____________________________
Richard Apodaca
Blog: http://depth-first.com
____________________________
Richard Apodaca
Blog: http://depth-first.com
____________________________________________________________________________________
We have the perfect Group for you. Check out the handy changes to Yahoo! Groups
(http://groups.yahoo.com)
More information about the rjb-users
mailing list