andkorn.org

a fine line between curiosity and madness.

Lua vs. Ruby in Java

Recently I have been working on sharpening my programming skills. I am a Network Engineer by trade but it never hurts to know how to program. I have colleagues that despise programming, but I have always found it to be interesting and fulfilling. Also, it does become very useful when you want to automate and monitor network devices. I am working on starting a new programming project and have been looking at Lua, Ruby, and Java as options for the new project. Each language has its pros and cons, and to make things even more complicated you can embed Lua and Ruby into Java. But embedding opens the door to many exciting possibilities.

Lua

I initially became interested in Lua about a year ago. Lua allows you to quickly and easily embed custom scripts into your C or C++ program. Lua can also be used on its own, and comes with extensive libraries, and more libraries via Lua Rocks. I never got as far as installing Lua, but instead read about it, read the tutorials, and read Programming in Lua by Roberto Ierusalimschy, the author of Lua. I could never wrap my head around the Lua to C interface nor Lua’s use of tables for everything. I guess there is just so much you can learn from reading. I have since installed Lua and regret not having begun working with it earlier!

Ruby

I then dropped the idea of Lua and began researching Ruby. Ruby is also an excellent language and can also be embedded in existing programs. Ruby also has much better beginner guides. I read and researched and then installed Ruby and JRuby. My new website is based on Octopress, which is written in Ruby. I have to say I really like Ruby and what it offers.

Choosing a scripting language

My new project will be Java-based, since Java has excellent cross-platform support and has an extensive library built-in. I am also going to include a scripting language such as Lua or Ruby – via LuaJ or JRuby, respectively. But which one to choose? Well there are pros and cons to each one, but I eventually chose Lua. Here is what I found, and why I chose Lua.

  • JRuby has an excellent installer and is very easy to install and get up and running. It comes with an IRB (Interactive Ruby Shell) so you can try out code snippets.
  • LuaJ comes as a zip file with some JARs and some example Java code and Lua scripts. You have to write a Java program to contain your Lua scripts. Despite this, I was up and running in about 5 minutes with LuaJ. The example code was very useful.
  • The JRuby download was 12MB compressed. To include JRuby in your project you have to include a 12MB JAR file with your code.
  • LuaJ was a 2MB download. The library to include in your project is 306KB.
  • Both JRuby and LuaJ can easily call native Java libraries.
  • While not a direct comparison, Lua code executes faster and uses much less memory than Ruby, in most cases. I can then assume that the Java-embeddable versions – LuaJ and JRuby – also compare in the same way. See Debian’s Computer Language Benchmarks Game Comparison
  • The JRuby IRB Shell takes about 15 seconds to start up on my computer. The LuaJ example programs start in much less time – just as fast as any Java program.

Once I added up all the pros and cons, there was a clear winner for my next project. I had to use Lua. I want my code to be small and light-weight, and that’s what Lua is all about. But! This doesn’t not mean I will drop Ruby completely! There are plenty of places where Ruby excels, such as website design with Ruby on Rails. Ruby also excels at scripting and can be used just as Perl and Python have been used as “glue” between technologies all these years.

Conclusion

You need to use the right tool for the right job. Or as I like to say, you should use a hammer where you need a hammer and a screwdriver where you need a screwdriver. Ruby, Python, and Perl are all hammers in the case of this project, while Lua is a lightweight screwdriver.

LuaJ’s swingapp.lua example program

See also

Comments