Ruby And DSLs And Blocks

by | Dec 13, 2017

Originally published: IT Jungle on 4/21/15

When I started with Ruby I was immediately drawn in by its syntax – simple, elegant, and easy to peruse. At the time I didn’t know this was an intentional mantra of the language put in place by Yukihiro “Matz” Matsumoto, Ruby’s creator. Matz said that he was “trying to make Ruby natural, not simple,” in a way that mirrors life. Further, Matz stated “Ruby is simple in appearance, but is very complex inside, just like our human body.” Let’s take this out of metaphor-land and into tangible code by introducing the DSL, or Domain Specific Language, aspect of Ruby.

First, what is a Domain Specific Language (DSL)? Martin Fowler describes it as “…a computer language that’s targeted to a particular kind of problem, rather than a general purpose language that’s aimed at any kind of software problem.” DSLs have existed for decades and you’ve probably been using them without even knowing it. For example, jQuery is an example of a DSL because it uses the Javascript language to define easier ways to do things specific things, like make AJAX calls. The SQL syntax is also a DSL because it is giving you a higher-level syntax with which to query the database.

In A Ruby and RPG Conversation, we introduced the xmlservice Gem and provided the following callable RPG program and corresponding Ruby invocation code.

One goal the xmlservice Gem team had was to create a DSL making it simpler and less verbose to make a call to programs. Looking at the above code we can see the “XMLService::” and “.new” text is repeated multiple times. What if we could get rid of that redundancy? We can. Below is a DSL to make program invocation simpler.

Author’s Note: The xmlservice Gem is now jointly maintained by IBM and the KrengelTech Litmis open source team at bitbucket.org/litmis/ruby-itoolkit

First we get an instance of the I_PGM Ruby object, which exists in the XMLService Ruby module. Next the DSL beauty comes into play when we invoke the input_parms method and pass in a Ruby block of the parameter definitions and initializing values.

What’s a Ruby block? In broader programming nomenclature it is a closure. Think of it as code that is sent to the method and invoked at a later point in time; in this case with what’s called the instance_eval method. Below is the input_parms method’s source code. Notice the &block being received into input_parms and subsequently used on the call to self.instance_eval. The instance_eval then evaluates the code and runs it in the context of itself, meaning, in this scenario, it looks for methods char and dec in this same Ruby program and invokes them.

Below is the char method definition from the xmlservice Gem. Notice how it still, in the end, invokes I_a.new as we saw in the original more verbose version of the Ruby code? This is how the DSL wrapped the more complex and verbose code to come up with the concise new syntax.

Ruby blocks and DSLs are used very frequently and in-favor over other language features. For example, the below shows a Ruby hash being iterated over using a Ruby DSL and block.

The same iteration could have been accomplished using Ruby’s for statement.

When I bring up the topic Ruby blocks during speaking engagements I always tell people “don’t worry if you don’t yet understand how the innards work, just be a good imitator of someone else’s code and copy/paste”. Or said another way: don’t worry if you don’t understand how Ruby blocks work under the covers and instead understand how to be a user of them. Here is another community reference on how DSLs in Ruby work if you’d like more in-depth examples. Stay tuned for more articles on all-things open source!

RPG-XML Suite

  • Call web APIs from RPG
  • Process XML and JSON
  • Offer web services

Litmis Spaces

Affordable IBM i cloud hosting

CyberSource Toolkit for i

  • Process credit cards from native RPG
  • Integrate CyberSource payment services

Follow Us

Need help with Ruby code? We’ve got you covered!

  • This field is for validation purposes and should be left unchanged.

Get Social

Share to your favorite social platform