« Return to Thread: JRuby Cookbook example 1.10

JRuby Cookbook example 1.10

by cag :: Rate this Message:

Reply to Author | View in Thread

I can not seem to get Example 1.10 to work using Netbeans 6.51 or 6.7. I create a Java Application project in Netbeans and pasted the code from the book in verbatim.

I was finally able to resolve org.jruby.Ruby by specifically adding
 C:\Program Files\NetBeans 6.7\ruby2\jruby-1.2.0\lib\jruby.jar
to the library options for the project - not ideal I think but it works

But that still left me with these errors:

package org.jrubycookbook.ch01;  // incorrect package name - also, is this perhaps where PrintJavaClass should reside?

public class PrintJavaClass // PrintJavaClass is public, it should be declared in a file named PrintJavaClass.java

List<Class> interfaces = Arrays.asList(o.getClass().getInterfaces()); // Incompatible types required java.util.List<java.lang.Class> found java.util.List<java.lang.Class<?>>


What environment where you using where you didn't get these errors?


Carl Graff wrote:
>
> Actually I did find instances of the jruby.jar but I think for some reason these are not considered path of the default classpath
>
> C:\Program Files\NetBeans 6.7\ruby2\jruby-1.2.0\lib\jruby.jar\org\jruby\Ruby.class
> C:\Program Files\NetBeans 6.5\ruby2\jruby-1.1.6\lib\jruby.jar\org\jruby\ Ruby.class
>
> Carl Graff wrote:

>> Hi,
>>
>> Unfortunately I only got to example 1.10 before getting stuck. I have used both Ruby and Java for several years but it is a little disheartening to get bogged down this early in the book.
>>
>> The sample code is below and here are some of my questions about it:
>> 1. Most importantly is is choking on
>> import org.jruby.Ruby;
>> I found a jruby.Jar on the JRuby site but I am using a complete Netbeans 6.7 download (Java and Ruby) and also tried adding Ruby and JRuby plugins to a 6.5 build. Nether of these builds know what to do with
>> import org.jruby.Ruby;
>> Do I need to download JRuby code - in addition - to the JRuby code and libraries provided by NetBeans? If so do I need to put this code in a special directory or add an environment setting or a netbeans option so it get recognized?
>>
>>
>> 2. The statement
>> package org.jrubycookbook.ch01;  Leads me to believe that either I need to download some associated sample programs or libraries or the I need to put this code in a specific directory relative to netbeans or perhaps relative to a separate installation of JRuby if that is needed - is either of these statements correct? If I need to download sample code or libraries the book does not seem to have a link to a download area.
>>
>> 3. The text preceding the sample indicates
>> Ruby Array objects can also be coerced into Java Array objects by calling the to_java method. Example 1-10 <javascript:moveTo('ruby_to_java_type_conversion');> includes a combination of Java and Ruby code, which demonstrates this functionality.
>> But I see no call to "to_java" in the sample code.
>>
>> package org.jrubycookbook.ch01;
>>
>> import java.io.PrintWriter;
>> import java.io.StringWriter;
>> import java.util.Arrays;
>> import java.util.Collections;
>> import java.util.List;
>>
>> import org.jruby.Ruby;
>> import org.jruby.javasupport.JavaEmbedUtils;
>>
>> public class PrintJavaClass {
>>
>>    // Output the class and interface list for a single object
>>    public String output(Object o) {
>>        String className = o.getClass().getName();
>>        List<Class> interfaces = Arrays.asList(o.getClass().getInterfaces());
>>
>>        return String.format("%s, implements %s\n", className, interfaces);
>>    }
>>
>>    // Output the class and interface list for each object in an array
>>    public String output(Object[] objects) {
>>        PrintWriter writer = new PrintWriter(new StringWriter());
>>        for (Object o : objects) {
>>            String className = o.getClass().getName();
>>            List<Class> interfaces = Arrays
>>                    .asList(o.getClass().getInterfaces());
>>
>>            writer.printf("%s (inside array), implements %s\n", className,
>>                    interfaces);
>>        }
>>        return writer.toString();
>>    }
>>
>>    public static void main(String[] args) {
>>        Ruby runtime = JavaEmbedUtils.initialize(Collections.EMPTY_LIST);
>>        String script = "@printer = org.jrubycookbook.ch01.PrintJavaClass.new\n"
>>                + "def output(o)\n"
>>                + "puts \"#{o.to_s} - #{@printer.output(o)}\"\n"
>>                + "end\n"
>>                + "output(1)\n"
>>                + "output(0.5)\n"
>>                + "output('string')\n"
>>                + "output(true)\n"
>>                + "output([4, 8, 15, 16, 23, 42])\n"
>>                + "output([4, 8, 15, 16, 23, 42].to_java)\n"
>>                + "output({ 'NY' => 'New York', 'MA' => 'Massachusetts'})\n";
>>
>>        runtime.evalScriptlet(script);
>>        JavaEmbedUtils.terminate(runtime);
>>    }
>> }


 « Return to Thread: JRuby Cookbook example 1.10