« Return to Thread: JRuby Cookbook example 1.10

Re: JRuby Cookbook example 1.10

by cag :: Rate this Message:

Reply to Author | View in Thread

Thank you Yoko for the careful evaluation and reply to my questions.
I think the one thing that I might be missing in all this is where you got the JARs or sample code for the solution. I am reading this book though my Safari books subscription and can not find the chapter based sample code nor the code implementing "PrintJavaClass".

Did you have sample code or create your own "PrintJavaClass"? Did you obtain any sample code for each chapter?
If it is case that I simple need to create  a package called org\jrubycookbook\ch01\PrintJavaClass.java I guess I have a lot to learn about creating "empty/new" package paths. Perhaps you would be kind enough to show me the correct way to create the package org\jrubycookbook\ch01.




Yoko Harada wrote:
Hi,

On Mon, Jun 29, 2009 at 10:41 PM, cag<cagraff@cox.net> wrote:
>
> 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.

This sample worked as far as I tried although the output was bit
different shown in the book.

>
> 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

You did in a very common way as described at
http://www.netbeans.org/kb/docs/java/project-setup.html#projects-classpath.

>
> But that still left me with these errors:
>
> package org.jrubycookbook.ch01;  // incorrect package name - also, is this
> perhaps where PrintJavaClass should reside?

Package hierarchies are mapped to directory(folder) hierarchies. The
example has org.jrubyvookbook.ch01 for its package name, so
PrintJavaClass.java file must reside under the org\jrubycookbook\ch01
directory. Probably,
http://java.sun.com/docs/books/tutorial/java/package/managingfiles.html
would help you to figure out what the package is.
Since you are using NetBeans, you don't need to create folders by
hand. http://www.netbeans.org/kb/docs/java/javase-intro.html might be
good for you.

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

Yes. Public classes should be in the same filenames, so your program
should be written in org\jrubycookbook\ch01\PrintJavaClass.java. You
can found a lot of information by googling since this is a basic part
of 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<?>>

I'm not sure why you got this error. The error might be resolved if
you could set up your Java project correctly on NetBeans.

-Yoko

>
>
> 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);
>>>    }
>>> }
>
>
>
> --
> View this message in context: http://www.nabble.com/JRuby-Cookbook-example-1.10-tp24263046p24263046.html
> Sent from the JRuby - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

 « Return to Thread: JRuby Cookbook example 1.10