Friday, February 25, 2011

JRuby and writing to mysql

Getting JRuby to talk to MySQL can be a reall PIA. Heres what you do.

First install the jdbc-mysql gem in JRuby. You do this by using the -S flag for JRuby. This goes for all gem commands. So to install this gem you would type 'jruby -S gem install jdbc-mysql'. As another example if you want to list your gems installed in JRuby you'd do 'jruby -S gem list'. Get the idea?

The way I'm making calls to mysql is by using a wrapper. I'm calling this mysql_helper.rb, and it looks like this:



Notice our require statement from the jdbc-mysql driver. Note the syntax is a little silly. While the gem itself is called jdbc-mysql, when we actually reference it with a require statement, we use jdbc/mysql.

Now to actually call this we just 'require' the mysql_helper.rb file in whatever script we are running.

Heres a self contained script:


So heres what the code would actually look like to do some stuff in your database using ruby and jruby.

Monday, November 1, 2010

HTMLUNIT and Ruby...

I'll use this blog to chronicle the setup and use of HTMLUNIT. HtmlUnit is a "GUI-Less browser for Java programs. More info can be found at http://htmlunit.sourceforge.net/

The first thing I did was a clean install of Ubuntu 10.10. I performed the standard updates after the first boot, so nothing special there.

Next we'll want to install ruby and jruby. We can use 'apt-get' for that. So run:
'sudo apt-get install ruby' which will install Ruby version 1.8.7. You can verify the installation by typing 'ruby -v'.

Now do the same for JRuby. JRuby allows for the implementation of Java using Ruby. To install JRuby type 'sudo apt-get install jruby'. This will give you version 1.5 of JRuby. You can verify the installation by typing 'jruby -v'.

Thats all we need as far as packages go. HTMLUnit is written in Java, so we do need Java installed for it to work. When you install Ubuntu you *should* get version 1.6.x version of Java. You can verify this by typing 'java -version'. If you do not have Java you should be able to run 'sudo apt-get install java' to install it.

Now lets get the latest version of HTMLUnit.

>Created htmlunit.rb which includes require statements for HTMLUnit jar files and class paths to HTMLUnit class paths that looks like this:

Couple of things to note here. We added the first require statement for htmlunit.rb. This give us access to the htmlunit JARs and Classes. Second, notice we added a require statement for 'test/unit'. This gives us access to Rubys unit testing framework. Also note our method name, 'test_main'. Method names using Rubys native test framework must begin with 'test'.

The actual test looks like this:


To run the test you just issue the command 'jruby test.rb'
Heres a looks a my directory structure:


Using Fedora?
If you are using Fedora rather than Ubuntu, there are few more hoops you need to jump through. I ran through this setup using a clean install of Fedora 13.

>install java su -c 'yum install java'
>install ruby su -c 'yum install ruby'
>install jruby; download tar(binary), and install to /usr/lib
>create symlink in /usr/bin pointing to /usr/lib/jruby/bin/jruby; su -c 'ln -s /usr/lib/jruby-1.6.0.RC2/bin/jruby jruby'

>