Thanks Gergo, that has enabled me to get it working.
In the first method I now have
receiver = container.runScriptlet(PathType.CLASSPATH, filename)
String method = “startMeUp”;
theClass = container.callMethod(receiver, method, Object.class);
and in the JRuby code the method “startMeUp” returns the class instance
it has created
and in the second method I have
container.callMethod(theClass, method, args, Double.class);
Gergely N. wrote in post #1094052:
Jeez - I thought this was a simple question…
Yeah, it should have been simple, you just missed 1 simple necessary
detail
to be able to help you (while getting a lot of unnecessary ones).
You found a method wasn’t working, and I was simple asking how you call
that method.
No, I had no non-working method because I didn’t even know how to get
that far when the JRuby method was in a class.
Also, to head off any digression, this is a scriptingContainer question
Action, this question doesn’t seem specific to ScriptingContainer or
even JRuby (or Android, as you said). It’s more about “how to instance
method in Ruby?”
The solution has shown that this was completely a ScriptingContainer
question - none of the documentation I have read has even hinted that
ScriptingContainer can be used like this. What other useful secrets has
it got?
Therefore the best is to understand a bit more of Ruby object model, eg
see
Programming Ruby: The Pragmatic Programmer's Guide.
So the simple answer to your simple question would be “to be able to
call a
method, first you need an object for it that can handle it”. I assume
that
would not make you happier, so I’ll try to clarify further.
The idea is to send a message to the receiver, where receiver is an
object which knows how to handle it and respond (in the lucky case).
What messages an object can handle depends on the methods defined in
its class (there’s more to that, but let’s keep it simple now).
You create an object of a class named CustomerClassSelector, by calling
.new method on the class (to answer your other question).
Yes, I already knew this as it applied to regular JRuby.
In your first attempt, you just defined a method on the class named
“Object” (the default place for top-level method).
Since this is the root of the class hierarchy all objects will have your
method.
IOW it does not matter what the receiver object is in your first case,
hence it works.
In your second one, you’re defining the method within a TestClass, so it
will be only available in objects of TestClass.
That’s why we were asking what your receiver object is.
I understand now, but I would have liked it if you had explained your
question.
I use the same receiver in the second method onActivityResult()
This is probably wrong, if I understand your snippets your receiver
object
is not a TestClass -in fact it’s probably a nil (which is an object of
NilClass).
Again, what you probably should do is the set the receiver there to the
result of “startmeup” , in this case the result of the first callMethod.
In pure Ruby, this would be
mytest = startmeup()
mytest.onActivityResult(…)
hope this helps,
Yes it has, thanks again
Gergo