-
DSLs in Ruby
Dave Burt
-
What Is A DSL?
- Not a general-purpose language
- No inscrutable, arcane syntax or geek-speak
- Not verbose for domain problems
-
Rubix Cube Moves
RL’FB’UD’RL’
-
HR Management
with Employee("123-45-6789") do
dock_salary 1000
warn_about :misconduct
end
-
Dungeon
class Dragon < Creature
life 1340 # tough scales
strength 451 # bristling veins
charisma 1020 # toothy smile
weapon 939 # fire breath
end
-
Database connection
development:
adapter: mssql
host: localhost
database: northwind
authentication: integrated
production:
development
-
Database table definition
create_table :updates {
foreign_key :user
foreign_key :group
text :body
string :type
timestamps!
}
-
HTML templating
html {
head {
title action_name
stylesheet_link_tag 'scaffold'
}
body {
p flash[:notice], :style => "color: green"
self << @content_for_layout
}
}
-
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
-
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
-
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
-
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
-
Types of DSL
- Brand new language
- Build on existing language
-
How to make a DSL
- Write code in the language
- Make it legal
-
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"
}
}
}
-
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
-
References
-
Please Ask Questions Now
Thanks for listening