• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Ruby And DSLs And Blocks

    April 21, 2015 Aaron Bartell

    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 Conversion we introduced the xmlservice Gem and provided the following callable RPG program and corresponding Ruby invocation code.

    --- MYLIB/PGM1 ---
     dcl-pr pgm1 extpgm; 
       char1 char(1);
       dec1 packed(7:4);
     end-pr;
     dcl-pi pgm1;
       char1 char(1);
       dec1 packed(7:4);
     end-pi;
    
     char1 = 'C';
     dec1 = 321.1234;
     return;
    
    
    pgm1 = XMLService::I_PGM.new("PGM1", 'MYLIB') <<
           XMLService::I_a.new('mychar1', 1, 'a') <<
           XMLService::I_p.new('mydec1', 7, 4, 11.1111) 
    pgm1.call
    

    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/xmlservice

    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.

    pgm1 = XMLService::I_PGM.new('PGM1','AARONLIB')
    pgm1.input_parms do
      char 'mychar1', 1, 'a'
      dec 'mydec1', 7, 4, 11.1111
    end
    

    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.

     def input_parms(&block)
       @xml_isParm = true
       self.instance_eval(&block)
       self
     end
    

    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.

     def char(var, size, data=" ")
        el = I_a.new(var,size,data)
        if @xml_isParm
          self.inputParameter(nil,nil,el)
        else
          self.setReturn("xchr",el)
        end
        el
     end
    

    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.

    my_hash = { one:   "value 1", 
                two:   "value 2", 
                three: "value 3"}
    
    my_hash.each do |key, value|
      puts "#{key} is #{value}"
    end
    

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

    for key, value in my_hash
      puts "#{key} is #{value}"
    end
    

    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 and here are two more Ruby community references on how DSLs in Ruby work if you’d like more in-depth examples. Stay tuned for more articles on all-things open source!

    Aaron Bartell is Director of IBM i Innovation for Krengel Technology, Inc. Aaron facilitates adoption of open source technologies on IBM i through professional services, staff training, speaking engagements, and the authoring of best practices within industry publications and www.litmis.com. With a strong background in RPG application development, Aaron covers topics that enable IBM i shops to embrace today’s leading technologies including Ruby on Rails, Node.js, Git for RPG source change management and RSpec for unit testing RPG. Aaron is a passionate advocate of vibrant technology communities and the corresponding benefits available for today’s modern application developers. Connect with Aaron via email at abartell@krengeltech.com. Aaron lives with his wife and five children in Southern Minnesota. He enjoys the vast amounts of laughter having a young family brings, along with camping and music. He believes there’s no greater purpose than to give of our life and time to help others.

    RELATED STORIES

    IBM i Toolkit for Ruby, iit For Short

    RubyGems Are The Foundation Of Success

    A Ruby And RPG Conversation

    Git To GitHub

    Git To It

    Knee-Deep In Ruby Waters

    Testing The Ruby Waters

    Bash Is Not A Shell Game

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Profound Logic Software:  Modernize 2015 @ COMMON. FREE Seminar. April 25, 8:30 a.m.
    BCD:  Free Webinar: Resolve PHP problems & optimize performance with Zend Server 8 & Z-Ray. April 21
    COMMON:  2015 Annual Meeting & Expo, April 26 - 29, at the Disneyland® Resort in Anaheim, California

    Observations From Oracle Collaborate 2015 The Remaining Power8 Systems Loom

    Leave a Reply Cancel reply

Volume 15, Number 08 -- April 21, 2015
THIS ISSUE SPONSORED BY:

WorksRight Software
CCSS
Storagepipe

Table of Contents

  • An SQL Pivot Procedure
  • Joining On Ranges
  • Ruby And DSLs And Blocks

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle