Why hide your Java with a DSL using JRuby?

The Java vs. Ruby dialogs are everywhere. I intend to show how you can use the complementary strengths of the Ruby language and the Java platform. This basically amounts to trying to avoid the Java language as much as possible, while leveraging the JVM and libraries. There is enough information around about the merits of Java, so I am going to focus on how Ruby can enable Java with new super powers.

The problem with the Java language is the lack of expressiveness. If you are naive enough to think that Java has no real problem here then do two things. First, keep reading while I give an example. Second, work with a better language for a while. Be warned that once you start down this path you will be permanently changed.

This will work a little like a magic trick. We will start with some Java code. As usual, java will look awkward and feel clumsy. Then we will use JRuby and some concepts that are missing from Java to make all the ugliness disappear. By the end we will have code that people can read or write without knowing a thing about Java or programming at all. Here's the Java code, check it over and make sure it's authentic.


This is simply a node in a tree. Sure it is contrived for this example, that's the point. It knows its name and the nodes below it. We can add children to it. This would be useful for building up a little hierarchy like this.


Java got the job done, but that's ugly. In fact I bet you skipped right over that code block. That's okay, I'll write it out like this so that you can easily see that it is a hierarchy of shape types.


Now the nested structure of shapes is pretty obvious at a glance. This is actually valid Ruby code. No matter how much more Java than Ruby you have written, our DSL is going to immediately be more readable to you. This is the point where it should start to dawn on you that this isn't about Java vs. Ruby, it's about using the tools available effectively.

A DSL is just an abstraction like a good API. While an API can insulate you from details with simpler concepts, a DSL can insulate you from unintuitive syntax and language features as well. Ruby has the ability to do this way better than Java can.

If you are just concerned with the business of using a hierarchy of things then you don't need to know Ruby or even Java now. The fact that this DSL is internal to Ruby, implemented in JRuby, and delegates to existing Java is just implementation details to the user. That means they don't need to know. Only the author of the DSL needs to know Ruby. Ruby does a lot for us since we don't have to write a parser and can use existing tools like IDE's.

One concern about DSL's is the creation of lots of mini languages and the investment to learn them. But there are mini languages everywhere. Picking up a DSL is similar to learning a new API or XML dialect.

In this case the DSL can potentially do much more than the original Java code. The DSL is focused only on describing the data structure while the original Java is tied to the action of building the structure. Separation of concerns here is useful because it lets you use the description for actions other than building. For example, I've used a similar DSL for making assertions about the structure of a tree and also writing queries against it. This works out especially handy in unit tests where you need a convenient way to express an initial structure and an expected value.

Hopefully I've convinced you that this is genuinely useful stuff. Java is ugly, and sometimes it is just better to hide it. A Ruby DSL can be a nice alternative. Now that the why is out of the way, next time I will focus on the how.

11 comments:

Anonymous said...

Interesting to know.

Government Jobs said...

Hey Your site is awesome and full of information. I have read you posts they are so informative. Keep Posting wonderful content.
Ani international provide the security solutions for all kind of secruity system and other equipment.
Home security system in jaipur
Wireless Home Security System in jaipur
Realtime attendance machine in jaipur
CCTV Camera dealer in jaipur
Hikvision DVR in jaipur at Rajasthan
security system solutions in jaipur
website design in jaipur
website development company in jaipur
seo company in jaipur

liza said...


Thank you for such a sweet tutorial - all this time later, I've found it and love the end result. I appreciate the time you spent sharing your skills.
Deer Hunting Tips Camping Trips Guide DEER HUNTING TIPS travel touring tips


liza said...


Thanks for sharing the information. It is very useful for my future. keep sharing

Still Hunting Method
Hunting psych tips Survival Tips Travel Touring Tips

Rajesh said...

nice post thank you so much for sharing this post useful to us
devops training in bangalore
devops training in btm layout
devops training institutes in bangalore
devops training institutes in btm layout
devops training in btm layout
devops certification course in BTM Layout
devops certification course in bangalore

Unknown said...

Jika anda bisa meningkatkan skill anda dalam bermain, maka itu akan jadi sebuah keuntungan yang bisa diandalkan sebetulnya. Apakah anda menginginkan semua itu
asikqq
http://dewaqqq.club/
http://sumoqq.today/
interqq
pionpoker
bandar ceme
freebet tanpa deposit
paito warna terlengkap
syair sgp

Chandra Sekhar Reddy said...

Excellent Information
Yaaron Studios is one of the rapidly growing editing studios in Hyderabad. We are the best Video Editing services in Hyderabad. We provides best graphic works like logo reveals, corporate presentation Etc. And also we gives the best Outdoor/Indoor shoots and Ad Making services.
Best video editing services in Hyderabad,ameerpet
Best Graphic Designing services in Hyderabad,ameerpet­
Best Ad Making services in Hyderabad,ameerpet­

TNK Design Desk said...

Nice blog! i'm also working with a Digital marketing company in gurgaon
graphic designing company in gurgaon
website company in gurgaon
best website designing company in india
top website designing company in india
website designing company in gurgaon
website development company in gurgaon
web development company in gurgaon
best website designing company in gurgaon
website designing services in gurgaon
web design company in gurgaon
website company in gurgaon
Website design Company in gurgaon
website designing in gurgaon
website designing company in gurgaon
website design in gurgaon
website design company in gurgaon
website design services in gurgaon
website design service in gurgaon
website designing in gurgaon
web design services in gurgaon
best website design company in gurgaon
best web design company in gurgaon
best website design in gurgaon
best website design services in gurgaon
best website designing services in gurgaon
best web design in gurgaon
best web design company in gurgaon
best web designing services in gurgaon
best web design services in gurgaon

William said...

All the time I found something new information from your blogs. Thanks for sharing .hey... Great work . I feel nice while i reading blog .You are doing well. Keep it up. We will also provide QuickBooks Customer Support Contact us 1-855-756-1077 for instant help.

Unknown said...

I have read your blog its very attractive and impressive. Nice information. It helped me alot. Fut Istanbul

Sarika A said...

I wanted to leave a little comment to support you and wish you the best of luck. We wish you the best of luck in all of your blogging endeavors. We are providing the best services click on below links to visit our website.
Oracle Fusion HCM Training
Workday Training
Okta Training
Palo Alto Training
Adobe Analytics Training