Mirah - Where Java Meets Ruby
When presented with the idea of Mirah (a language based on the JVM languages but also borrowing heavily from Ruby) it would seem we are being given a more palatable, less enterprise way of looking at Java. Mirah hopes to eventually be a replacement for Java, though there is still a large amount of work to be done before reaching that goal.
Mirah, originally called Duby, was created by Charles Nutter. Started in 2009, Charles wanted “a ‘Ruby-like’ language, probably a subset of Ruby syntax, that [could] compile to solid, fast, idiomatic JVM bytecode.” The word itself means ruby (the actual gemstone) in Javanese, the national language of Java.
At this point, it is important to note that the Mirah language is only at version 0.0.11 and is still in the very beginning stages of development. And you should keep this in mind when considering any of its shortcoming. As a matter of fact, finding ways to fix these shortcomings would be a great way to give back to this open source project.
According to the Mirah site, the language is based on a few guiding principles:
- No runtime library - this means no jar files, dependencies can be decided by the developer and therefore customized to each project, similar to the way bundler helps Ruby projects to remain siloed
- Clean and simple syntax - static typing and syntax similar to the JVM type system, but at the same time borrowing heavily from Ruby
- Metaprogramming and macros - built-in tools for macros and metaprogramming at compile time means you get the feel of a dynamic language while still being a compiled language
- No performance penalty - “Because Mirah directly targets the JVM’s type system and JVM bytecode, it performs exactly as well as Java.” This is quoted directly from the site and seems to be neither a positive or negative attribution to Mirah itself.
At this point, we need to get started to really see what it is we are dealing with in Mirah.
Getting Started
Beginning with any language can often be the biggest hurdle. With Mirah, the authors have given us the opportunity to find our own way by offering a few different methods. Regardless of the method used, JRuby 1.6.0 or greater and BiteScript 0.0.8 or greater are necessary. After that you can install using any of the following methods:
- Zip file - Download the latest stable release. Extract it, and add
bin
to your$PATH
to be able to usemirah
,mirahc
, etc. - Build from source (this is the method I took) - take the following steps to build from source:
$ git clone http://github.com/mirah/mirah.git
$ cd mirah
$ bundle install
$ rake gem
$ gem install pkg/mirah-*.gem
- Java tools - if you really love jar files and are not willing to move away from them, run “rake jar” in the mirah directory.
- Maven - there is a specific sample project for maven users to check out
NOTE: According to the documentation, you should be able to gem install mirah be using jruby -S gem install mirah for installation. When attempting this, there were several errors. It seems easier to use RVM to create a jruby environment and set up naturally in JRuby.
Once everything is installed simply run
mirah -e 'puts "Hello World"'
to ensure everything is working as expected.
Using Mirah
While there are some simple examples provided in the mirah directory once installed, most will want to try some things out on their own in order to kick the tires. For this example, we will take some simple input, a name, and count the characters it.
puts "Enter a name: "
s = System.console.readLine()
puts "The name you entered was " + s
name_length = s.length
puts "Your Name is " + name_length + " letters long"
A simple example, and it gives us the following output:
Look over the examples provided and try to get an example of your own put together based on what is found in the /examples/rosettacode folder. This is the best way to start getting comfortable with the way Mirah works.
Some pitfalls to keep in mind. If you are used to a dynamic language, like Ruby, Mirah will seem slow. Mainly that’s because it needs to compile, something Ruby never needs to do. Also, the errors received when something goes wrong are not in the form of the stacktrace Rubyists are used to seeing. This is merely, inconvenient. After a few errors (and everyone has a few starting up) they become easy to understand and troubleshoot. Mirah has the potential to bring together the best of both worlds, Java and Ruby. The syntax is simple to use and the compiled aspect helps to eliminate the “gotchas” sometimes inherent in using dynamic languages. While there is still a bit of a road, Mirah is being used in production applications so it is ready for primetime, though it is recommended to wait for a decent 1.0.0 version before using it in production.
Share your thoughts with @engineyard on Twitter