Ronald,
You can pass arbitrary java objects. In my case, I arbitrarily passed
in an array list, and using this implementation I obfuscated that fact:
def doIt(array_list)
r_val = []
array_list.each do |s|
r_val << s + “: Rubified!!”
end
r_val
end
JRuby can use dark magic to do this, I could just as easily do this:
def doIt(array_list)
# r_val = []
# array_list.each do |s|
# r_val << s + “: Rubified!!”
# end
# r_val
r_val = java.util.ArrayList.new
it = array_list.iterator
while (it.hasNext)
n = it.next
r_val.add(n << “: Still rubified!!!”)
end
r_val
end
now I am treating the ArrayList as any old POJO.
Things get dicey if we have overloaded methods in java as Ruby does not
allow this. If you run into this problem look up java_send. Here is a
quick example:
irb(main):001:0> a = java.lang.StringBuilder.new
=> #Java::JavaLang::StringBuilder:0x443118b0
irb(main):002:0> a.java_send :append, [java.lang.String], “I am Here\n”
=> #Java::JavaLang::StringBuilder:0x443118b0
irb(main):003:0> puts a
I am Here
=> nil
irb(main):004:0> a.java_send :append, [java.lang.CharSequence], “I am
not here!\n”
=> #Java::JavaLang::StringBuilder:0x443118b0
irb(main):005:0> puts a
I am Here
I am not here!
As far as bundling with a jar…
making the following change in the runScript method:
//DoIt it =
(DoIt)container.runScriptlet(org.jruby.embed.PathType.ABSOLUTE,“c:\temp\foo.rb”);
DoIt it =
(DoIt)container.runScriptlet(org.jruby.embed.PathType.CLASSPATH,“foo.rb”);
and placing foo.rb in a jar file on the classpath will work.
The only way I can think of to completely hide your ruby source code is
to place the string in a java file and call the runScriptlet method with
a string argument.
If I take my original foo.rb and compile it here is the decompiled code:
import org.jruby.Ruby;
import org.jruby.ir.IRScope;
import org.jruby.ir.runtime.IRRuntimeHelpers;
public class foo
{
public static void main(String args[])
{
Ruby ruby = Ruby.newInstance();
ruby.runInterpreter(IRRuntimeHelpers.decodeScopeFromBytes(ruby,
script_ir.getBytes(“ISO-8859-1”), “foo.rb”));
}
public static IRScope loadIR(Ruby ruby, String s)
{
return IRRuntimeHelpers.decodeScopeFromBytes(ruby,
script_ir.getBytes(“ISO-8859-1”), s);
}
private static final String script_ir = (new
StringBuilder()).append("\000\000\000\001\000\000\0014\377\377\377\377\016\b\023t\000\000Nt\006\001s\000Nt\005\002_\000?\000Nt\000\003{\000fNl\001h\000t\000\003?\001Gt\000\004f\030f/f\037\000\004to_at\000\004\000t\000\005\024\000\004eacht\000\005\377\377\377\377\377wS\001t\000\006?\005\035\001\001pS\001l\001h\000t\000\007%t\000\007\0232L\025_GLOBAL_ENSURE_BLOCK_\000\b\023t\004\000\001\tl\001i\000\000Nt\006\001s\000Nt\005\002_\0001L\016_CLOSURE_START\000?\002\033\000\001-l\001i\000\001f\030t\004\003\001\033\000\001-l\001i\000\001f\004t\004\004\001\033\000\001%t\004\004\001\001f\030t\004\005\001\026l\001h\001\003[]=\002t\004\003\001t\004\005\001%t\004\005\00131L\025_GLOBAL_ENSURE_BLOCK_\000\020t\004\006\001Wt\004\007\001\002\001t\004\006\001%t\004\007\0011L\007CL1_LBL\000\002\007\006foo.rb\000\b\000\000\001\001h\000\001\001h\000\f\000\020foo.rb_CLOSURE_1\001\b\003\f_CLOSURE_END\001\016_CLOSURE_START\001\007CL1_LBL\001\000\377\000\000\002\000\000\000\000\000\001\001\001i\377\000\000\002\000\000\000\000\000\001\001i\000p").toString();
}
We can see script_ir is private and taunting us.
You could probably write a ruby script that converts ruby scripts to be
member escaped strings in a java file if it is that
important.
Good Luck,
Cris