1. DSLs in Ruby

    Dave Burt

  2. What Is A DSL?

  3. Rubix Cube Moves

    RL’FB’UD’RL’
    
  4. HR Management

    with Employee("123-45-6789") do
      dock_salary 1000
      warn_about :misconduct
    end
    
  5. Dungeon

    class Dragon < Creature
      life 1340     # tough scales
      strength 451  # bristling veins
      charisma 1020 # toothy smile
      weapon 939    # fire breath
    end
    
  6. Database connection

    development:
      adapter: mssql
      host: localhost
      database: northwind
      authentication: integrated
    
    production:
      development
      
  7. Database table definition

    create_table :updates {
      foreign_key :user
      foreign_key :group
    
      text   :body
      string :type
    
      timestamps!
    }
    
  8. HTML templating

    html {
      head {
        title action_name
        stylesheet_link_tag 'scaffold'
      }
    
      body {
        p flash[:notice], :style => "color: green" 
        self << @content_for_layout
      }
    }
    
  9. Interdependendent tasks

      task :all => ['hello']
    
      file 'main.o' => ["main.c", "greet.h"] do
        sh "cc -c -o main.o main.c"
      end
    
      file 'greet.o' => ['greet.c'] do
        sh "cc -c -o greet.o greet.c"
      end
    
      file "hello" => ["main.o", "greet.o"] do
        sh "cc -o hello main.o greet.o"
      end
      
  10. Invoicing

    new_invoice do
      address 'Test Client, Street, ZIP'
      code '2006-45'
      date '3rd Oct'
    
      at_rate 50 do
        spent 3.hours_on('Morning meeting')
        spent 0.5.hours_on('Making the coffee for the boss').on('3rd Sept')
      end 
    end
    
  11. Workflow

    route "Problems" do
      step "Problem Resolution"
    end
    
    route "EMail Order" do
      step "Validate Customer"
      step "Assemble Order"
      step "Charge Credit Card" do
        on "invalid", :route => "Problems", :step => "Problem Resolution"
      end
      step "Ship"
    end
    
  12. Conversation

    >> I.say "I love the Ruby language”
    You said, ‘I love the Ruby language’
    
    >> You.cannot :learn, “Ruby”
    Didn’t your mother tell you never say never? Of course you can learn Ruby
    
  13. Types of DSL

  14. How to make a DSL

    1. Write code in the language
    2. Make it legal
  15. Let's make one

    Write code in the language

    slideshow {
      slide "How to make a DSL" {
        list {
          item "Write code in the language"
          item "Make it legal"
        }
      }
    }
    
  16. How will our language be used?

    What do we want it to do?
    Generate XHTML like this slideshow here
    How will we make that happen?
    Running the program using the Ruby interpreter will dump the XHTML on standard output
  17. References

  18. Please Ask Questions Now

    Thanks for listening