On Tue, Jun 4, 2013 at 1:23 PM, Ashwin Jayaprakash
<
ashwin.ja...@gmail.com> wrote:
> Hi, I was trying out ways to create a micro-DSL using JRuby.
>
> A) I wanted to do instance_eval on a Java class. On my first attempt I got
> this warning message: "warning: singleton on non-persistent Java type
> Java::JrDemo::Car (
http://wiki.jruby.org/Persistence)"
instance_eval causes an object to create a singleton class, which
needs to be kept attached to the object from then on. Because we can't
attach our own objects to arbitrary Java objects, singletonizing Java
objects is deprecated and will probably go away in the future. That's
why you're getting this warning for this case.
The warning it self basically means that the Java object in question
will not necessarily have the same Ruby object wrapper every time you
see it, so singleton classes, instance variables, and so on may not
always accompany it. You can set the class to be "persistent" by
calling:
Java::jr.demo.Car.__persistent__ = true
It will cost more to work with the object (since we have to maintain
the same wrapper everywhere) but the wrapper will always be the same,
singletons and instance variables will accompany it, and you won't get
the warning anymore.
> B) After reading the instructions on that Persistence page, I tried
> sub-classing the Java class inside JRuby. I also wanted to do the
> instance_eval inside the initialize method. When I ran the code, it barfed
> with this exception - "Java wrapper with no contents: #<Class:0x5584d9c6>"
When extending a Java class, you need to call super to create the Java
half of the object (initialize handles the Ruby half). We have tried
to add better error messages for this, but I think in this case our
normal error message isn't reached because you try to instance_eval
before the Java object is created.
> C) To make sure that I wasn't doing something stupid, I did the
> instance_eval in a non-initialize method and it worked:
In this case, the Java object is created fine, and since we're already
tying the Java object and a Ruby wrapper together permanently (via the
extension logic), persistent behaviors like singletons and instance
variables work without warning.
> D) Just to be sure if it was a JRuby peculiarity or a Ruby thing, I tried a
> pure Ruby class and everything worked!
Ruby objects are just Ruby objects, and we can attach a singleton
class to them at any time...so instance_eval works.
> What do you guys think? Am I doing something wrong or is the JRuby doc
> missing this information?
It certainly could be a documentation gap. Obviously the page on
persistence didn't tell you what you needed to know, eh?
- Charlie
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email