SlideShare a Scribd company logo
Ruby DSL
Crafting beautiful code
● You know basic Ruby
● OR get you interested to know Ruby
● Feel free to ask questions
● Will only scratch the surface
● After this, you can write a basic Ruby DSL
Expectations
DSL !!!
describe '#destroy'
context 'when resource is found'
it 'has 200 status code if logged in'
expect response to respond with 200
end
end
end
DSL ???
Test
X
DSL you might have seen in Ruby
describe '#destroy' do
context 'when resource is found' do
it 'has 200 status code if logged in' do
expect(response).to respond_with 200
end
end
end
RSpec
DSL you might have seen in Ruby
create_table :users do |t|
t.string :name
t.attachment :avatar
t.timestamps
end
DB Migrations
DSL you might have seen in Ruby
Rails.application.routes.draw do
root 'pages#home'
resources :pages, only: [:index, :show]
end
Rails Routing
What is DSL??
API that is tailored to
express Domain Logic
Language
VS
DSL
Why DSL ???
● Simple to express
● Embraces domain in code
● Powerful abstraction that allows you to
change
Let’s start
Keys to Ruby DSL
Code blocks
instance_eval
Code blocks
Ruby code block
def report
puts "Header"
yield
puts "Footer"
end
report do
puts "From block"
end
Code Output
Header
From block
Footer
REPL
instance_eval
class Report
def initialize(&block)
puts "Header"
instance_eval &block
puts "Footer"
end
end
Report.new do
puts "From block"
end
Ruby instance_eval
class Report
def initialize(&block)
puts "Header"
instance_eval &block
puts "Footer"
end
def my_print(str)
puts str
end
end
Ruby instance_eval - 02
Report.new do
my_print "From block"
end
REPL
class Report
def initialize(data, &block)
@data = data; @columns = []
instance_eval &block
end
def column(column_name) @columns << column_name end
def print
@data.each { |row| @columns.each { |column| puts row[column] } }
end
end
Ruby instance_eval - 03
data = [
{name: 'Jitu', age: 34},
{name: 'Razeen', age: 3}
]
report = Report.new(data) do
column :name
end
report.print()
Ruby instance_eval - 03
Jitu
Razeen
REPL
My experience
In one of the rails project I worked on had a tons of reports, which needed the
following features
● Queries, which are easy to understand and change
● Filters
● Pagination
● Generate PDF, CSV, and email those reports
● Generate graph in HTML and in PDF
● Had complex rowspan and colspan
DSL for generating reports
def index
reporter(Invoice.scoped) do
filter :title, type: :text
filter :created_at, type: :date
column :title { |invoice| link_to invoice.title, invoice }
column :total_paid, show_total: true
column :total_charged, show_total: true
column :paid
end
end
Demo
Author
A.K.M. Ashrafuzzaman
Software Engineer,
Newscred.
http://ashrafuzzaman.github.io
References
Link to this slide
Blogs
● DSL QandA by Martin Fowler
● Creating a Ruby DSL, by Leigh Halliday
Source code
● Source codes for this slide
● query_report gem

More Related Content

PPTX
RubyConf Bangladesh 2017 - Craft beautiful code with Ruby DSL
Ruby Bangladesh
 
PDF
Ruby Isn't Just About Rails
Adam Wiggins
 
PDF
Adventurous Merb
Matt Todd
 
KEY
Rails and Legacy Databases - RailsConf 2009
Brian Hogan
 
PDF
Domain Driven Rails
Yan Pritzker
 
PDF
Fast Web Applications Development with Ruby on Rails on Oracle
Raimonds Simanovskis
 
PDF
Game Development with Corona SDK and Lua - Lua Workshop 2014
SergeyLerg
 
PPTX
.NET Foundation, Future of .NET and C#
Bertrand Le Roy
 
RubyConf Bangladesh 2017 - Craft beautiful code with Ruby DSL
Ruby Bangladesh
 
Ruby Isn't Just About Rails
Adam Wiggins
 
Adventurous Merb
Matt Todd
 
Rails and Legacy Databases - RailsConf 2009
Brian Hogan
 
Domain Driven Rails
Yan Pritzker
 
Fast Web Applications Development with Ruby on Rails on Oracle
Raimonds Simanovskis
 
Game Development with Corona SDK and Lua - Lua Workshop 2014
SergeyLerg
 
.NET Foundation, Future of .NET and C#
Bertrand Le Roy
 

What's hot (20)

PPTX
PostgREST Design Philosophy
begriffs
 
PDF
The CQRS diet
Luismi Cavallé
 
PDF
LINQ Inside
jeffz
 
PDF
using Mithril.js + postgREST to build and consume API's
Antônio Roberto Silva
 
PDF
ClojureScript Introduction
Falko Riemenschneider
 
PPTX
An Evening Of DSLs: Microsoft Oslo
James Lynch
 
PDF
Advanced Reflection in Pharo
Marcus Denker
 
PPTX
Declarative JavaScript concepts and implemetation
Om Shankar
 
PDF
Bldr: A Minimalist JSON Templating DSL
Alex Sharp
 
PPTX
From Ruby to Scala
tod esking
 
PDF
Modern Web Development with Perl
Dave Cross
 
PDF
"Simple Made Easy" Made Easy
Kent Ohashi
 
PDF
RoR 101: Session 1
Rory Gianni
 
PDF
Reference Semantics with C# and .NET Core
Christian Nagel
 
PPT
Ruby Basics
SHC
 
PDF
Continuous Integration & Continuous Delivery
Konstantin Chukhlomin
 
PDF
NoSQL Yes, But YesCQL, No?
Eric Evans
 
PDF
Gourmet Service Object
Brooklyn Zelenka
 
ODP
Deploying Perl apps on dotCloud
daoswald
 
PostgREST Design Philosophy
begriffs
 
The CQRS diet
Luismi Cavallé
 
LINQ Inside
jeffz
 
using Mithril.js + postgREST to build and consume API's
Antônio Roberto Silva
 
ClojureScript Introduction
Falko Riemenschneider
 
An Evening Of DSLs: Microsoft Oslo
James Lynch
 
Advanced Reflection in Pharo
Marcus Denker
 
Declarative JavaScript concepts and implemetation
Om Shankar
 
Bldr: A Minimalist JSON Templating DSL
Alex Sharp
 
From Ruby to Scala
tod esking
 
Modern Web Development with Perl
Dave Cross
 
"Simple Made Easy" Made Easy
Kent Ohashi
 
RoR 101: Session 1
Rory Gianni
 
Reference Semantics with C# and .NET Core
Christian Nagel
 
Ruby Basics
SHC
 
Continuous Integration & Continuous Delivery
Konstantin Chukhlomin
 
NoSQL Yes, But YesCQL, No?
Eric Evans
 
Gourmet Service Object
Brooklyn Zelenka
 
Deploying Perl apps on dotCloud
daoswald
 
Ad

Similar to Ruby DSL (20)

PDF
Building DSLs On CLR and DLR (Microsoft.NET)
Vitaly Baum
 
KEY
Wider than rails
Alexey Nayden
 
PDF
Ruby on Rails Presentation
Michael MacDonald
 
PPT
Introduction to Ruby on Rails
Manoj Kumar
 
PPT
Ruby on Rails introduction
Tran Hung
 
PDF
How DSL works on Ruby
Hiroshi SHIBATA
 
PDF
Ruby On Rails Introduction
Thomas Fuchs
 
PDF
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
lennartkats
 
PDF
Otimizando Aplicações em Rails
Juan Maiz
 
PPTX
Ruby -the wheel Technology
ppparthpatel123
 
PDF
"ClojureScript journey: from little script, to CLI program, to AWS Lambda fun...
Julia Cherniak
 
PPT
Rochester on Rails: Introduction to Rails
Jason Morrison
 
PDF
Introduction to Rails - presented by Arman Ortega
arman o
 
PDF
The Skinny on Slim
Eric Mulligan
 
PPT
Ruby on Rails workshop for beginner
Umair Amjad
 
KEY
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
Matt Gauger
 
PDF
DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009
Harshal Hayatnagarkar
 
PDF
Ruby on Rails (RoR) as a back-end processor for Apex
Espen Brækken
 
PPTX
Road to sbt 1.0 paved with server
Eugene Yokota
 
Building DSLs On CLR and DLR (Microsoft.NET)
Vitaly Baum
 
Wider than rails
Alexey Nayden
 
Ruby on Rails Presentation
Michael MacDonald
 
Introduction to Ruby on Rails
Manoj Kumar
 
Ruby on Rails introduction
Tran Hung
 
How DSL works on Ruby
Hiroshi SHIBATA
 
Ruby On Rails Introduction
Thomas Fuchs
 
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
lennartkats
 
Otimizando Aplicações em Rails
Juan Maiz
 
Ruby -the wheel Technology
ppparthpatel123
 
"ClojureScript journey: from little script, to CLI program, to AWS Lambda fun...
Julia Cherniak
 
Rochester on Rails: Introduction to Rails
Jason Morrison
 
Introduction to Rails - presented by Arman Ortega
arman o
 
The Skinny on Slim
Eric Mulligan
 
Ruby on Rails workshop for beginner
Umair Amjad
 
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
Matt Gauger
 
DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009
Harshal Hayatnagarkar
 
Ruby on Rails (RoR) as a back-end processor for Apex
Espen Brækken
 
Road to sbt 1.0 paved with server
Eugene Yokota
 
Ad

Recently uploaded (20)

PDF
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
Immersive experiences: what Pharo users do!
ESUG
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Immersive experiences: what Pharo users do!
ESUG
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 

Ruby DSL

  • 2. ● You know basic Ruby ● OR get you interested to know Ruby ● Feel free to ask questions ● Will only scratch the surface ● After this, you can write a basic Ruby DSL Expectations
  • 4. describe '#destroy' context 'when resource is found' it 'has 200 status code if logged in' expect response to respond with 200 end end end DSL ??? Test X
  • 5. DSL you might have seen in Ruby describe '#destroy' do context 'when resource is found' do it 'has 200 status code if logged in' do expect(response).to respond_with 200 end end end RSpec
  • 6. DSL you might have seen in Ruby create_table :users do |t| t.string :name t.attachment :avatar t.timestamps end DB Migrations
  • 7. DSL you might have seen in Ruby Rails.application.routes.draw do root 'pages#home' resources :pages, only: [:index, :show] end Rails Routing
  • 8. What is DSL?? API that is tailored to express Domain Logic
  • 10. Why DSL ??? ● Simple to express ● Embraces domain in code ● Powerful abstraction that allows you to change
  • 12. Keys to Ruby DSL Code blocks instance_eval
  • 14. Ruby code block def report puts "Header" yield puts "Footer" end report do puts "From block" end Code Output Header From block Footer REPL
  • 16. class Report def initialize(&block) puts "Header" instance_eval &block puts "Footer" end end Report.new do puts "From block" end Ruby instance_eval
  • 17. class Report def initialize(&block) puts "Header" instance_eval &block puts "Footer" end def my_print(str) puts str end end Ruby instance_eval - 02 Report.new do my_print "From block" end REPL
  • 18. class Report def initialize(data, &block) @data = data; @columns = [] instance_eval &block end def column(column_name) @columns << column_name end def print @data.each { |row| @columns.each { |column| puts row[column] } } end end Ruby instance_eval - 03
  • 19. data = [ {name: 'Jitu', age: 34}, {name: 'Razeen', age: 3} ] report = Report.new(data) do column :name end report.print() Ruby instance_eval - 03 Jitu Razeen REPL
  • 20. My experience In one of the rails project I worked on had a tons of reports, which needed the following features ● Queries, which are easy to understand and change ● Filters ● Pagination ● Generate PDF, CSV, and email those reports ● Generate graph in HTML and in PDF ● Had complex rowspan and colspan
  • 21. DSL for generating reports def index reporter(Invoice.scoped) do filter :title, type: :text filter :created_at, type: :date column :title { |invoice| link_to invoice.title, invoice } column :total_paid, show_total: true column :total_charged, show_total: true column :paid end end
  • 22. Demo
  • 24. References Link to this slide Blogs ● DSL QandA by Martin Fowler ● Creating a Ruby DSL, by Leigh Halliday Source code ● Source codes for this slide ● query_report gem