SlideShare ist ein Scribd-Unternehmen logo
1 von 135
Downloaden Sie, um offline zu lesen
The Basis of Making DSL
                      with Ruby




2010   8   28
Who I am


                •   Yasuko Ohba
                •   Ruby Programmer
                •   Developing rails applications
                    and iPhone applications
                •   Everyleaf Corporation
2010   8   28
Housekeeping Book on Web
                             Kozuchi

                • http://www.kozuchi.net
                • http://github.com/everyleaf/kozuchi


2010   8   28
•   @nay3 (twitter)
                •   http://github.com/nay
                •   y.ohba@everyleaf.com


2010   8   28
2010   8   28
2010   8   28
2010   8   28
2010   8   28
2010   8   28
2010   8   28
2010   8   28
Ruby is good for
                      DSL




2010   8   28
Domain Specic
                  Language



2010   8   28
DSL

2010   8   28
DSL
                Hospitals




2010   8   28
Libraries




                                   DSL
                Hospitals




2010   8   28
Libraries         Game




                                   DSL
                Hospitals




2010   8   28
Libraries         Game




                                   DSL
                Hospitals                       EC




2010   8   28
It looks like a
                language rather than a
                        program



2010   8   28
DSL



2010   8   28
internal
                           DSL



2010   8   28
external


                internal
                              DSL



2010   8   28
internal DSL
                   in Ruby




2010   8   28
DSL examples


                •   Rails
                 • routes.rb
                 • migrations
                 • RJS
                • RSpec
                • Rake
2010   8   28
DSL-like codes : Rails


                class NotesController
                  before_lter :nd_group
                  ...
                end
2010   8   28
before_lter :nd_group




2010   8   28
It doesn’t
                look like DSL ?




2010   8   28
it can be less DSL like



                self.add_before_lter_methods(
                   :nd_group)


2010   8   28
much more DSL like


                self.add_before_lter_methods(
                   :nd_group)

                before_lter :nd_group
2010   8   28
My talk is simple




2010   8   28
I’ll talk about
                readable codes




2010   8   28
DSL-like codes : migrations


                create_table :users do |t|
                 t.string :name
                 t.timestamps
                end
2010   8   28
DSL-like codes : RSpec


                describe Group do
                 it “name                 ” do
                  g = Group.new
                  g.should_not be_valid
                 end

2010   8   28
Easy to read




2010   8   28
Easy to write




2010   8   28
Brief




2010   8   28
Easy to maintain




2010   8   28
OK, but...




2010   8   28
Do we just use
                existing great DSLs ?



                             Photo by Lawrence OP
2010   8   28
NO
2010   8   28
Write your ruby
                codes like DSL
                   everyday



2010   8   28
That’s just a skill of
                programming in Ruby




2010   8   28
I’m going to talk
                     about




2010   8   28
..how to make your
                   codes like DSL




2010   8   28
Our goal




2010   8   28
DSL-likeness




2010   8   28
What decides
                whether it is DSL
                    or not ?



2010   8   28
No Solid Boundary




2010   8   28
But we can feel it




2010   8   28
to make the
                difference clear




2010   8   28
Compare normal
                Ruby codes and DSLs




2010   8   28
Normal Ruby Style



                “Hey receiver, do/return THIS !”




2010   8   28
Hey receiver do THIS!

                array.clear
                array[1]
                array.collect!{...}
                hash.delete(key)
                string.empty?
2010   8   28
Because it’s OOP




2010   8   28
DSLs have
                another styles




2010   8   28
3 typical forms




2010   8   28
1.Declarative Programming

                2.Using Blocks

                3.Methods Represent Special
                  Concepts


2010   8   28
1. Declarative
                Programming



2010   8   28
“I am a rubyist”




2010   8   28
examples : declarative expression



                validates_presence_of :name
                before_lter ...
                has_many :children




2010   8   28
typical features


                • describe status or nature
                • receivers are not always important
                • no brackets
2010   8   28
No money


                self.money = 0




2010   8   28
No money


                self.money = 0

                poor

2010   8   28
Mostly in Class
                 Denitions




2010   8   28
No money


                class Boy
                  poor
                  ...
                end
2010   8   28
Try to express your
                request as a nature
                    of the class



2010   8   28
Object          task

                                 task
                         task




2010   8   28
the class has this nature




                Object          task

                                 task
                         task




2010   8   28
Modules are good
                to express natures


                Class         Object




2010   8   28
Modules are good
                     to express natures


                         Class     Object




                Module

2010   8   28
Modules are good
                     to express natures


                         Class       Object




                Module      Module

2010   8   28
implementation




2010   8   28
Example 1

                Declare the class to have
                some nature implemented as
                a module



2010   8   28
Book
                (Class)
                          class Book
                            include "Product"
                            ...
                          end
                Product

2010   8   28
class Book
                  include "Product"
                  ...
                end


                class Book
                  acts_as_product
                  ...
                end
2010   8   28
class Book
                   acts_as_product
                   ...
                 end

                 NameError: undened local variable or
                method `acts_as_product' for Book:Class


2010   8   28
You need
                acts_as_product
                     method



2010   8   28
in the super class




2010   8   28
Book       Object
                (Class)     (Class)
                          acts_as_product




2010   8   28
Book       Object
                (Class)     (Class)
                          acts_as_product




2010   8   28
Book       Object
                (Class)     (Class)
                          acts_as_product



                Product



2010   8   28
the easier way

                       class Object
                         def self.acts_as_product
                          include Product
                         end
                       end
                the caller (Book) will be self here


2010   8   28
Book
                (Class)
                          class Book
                            acts_as_product
                            ...
                          end
                Product

2010   8   28
class Object
                          def self.acts_as_product
                           include Product
                          end
                        end

                Execute this before acts_as_product is invoked


                Rails       cong/initializer/acts_as_product.rb


2010   8   28
the softer way

                module ActsAsProduct
                 module ClassMethods
                  def acts_as_product
                    include Product
                  end
                 end
                 def self.included(base)
                  base.extend(ClassMethods)
                 end
                end
                Object.instance_eval { include ActsAsProduct }

2010   8   28
Object
                 (Class)


                ActsAsProduct




2010   8   28
Object
                  (Class)

                                    add
                ActsAsProduct acts_as_product
                 ClassMethods




2010   8   28
Book       Object
                (Class)     (Class)

                                              add
                          ActsAsProduct acts_as_product
                           ClassMethods




2010   8   28
Book            Object
                (Class)          (Class)
                          call
                    acts_as_product
                                                  add
                              ActsAsProduct acts_as_product
                                ClassMethods




2010   8   28
Book             Object
                (Class)           (Class)
                           call
                     acts_as_product
                                                   add
                               ActsAsProduct acts_as_product
                Product
                                 ClassMethods




2010   8   28
Example 2

                set @title for a layout in some
                actions in Rails controllers




2010   8   28
Request
                          Controller
                index      show            new     edit




                                  @title
                        layout
2010   8   28
want to write like this


                class BooksController < ApplicationController
                  title "        ", :only => [:edit, :update]
                 ....
                end



2010   8   28
Same Strategy




2010   8   28
add a class method
                 in the super class




2010   8   28
ApplicationController

                title

                  BooksController

2010   8   28
ApplicationController

                class ApplicationController < ...
                  def self.title
                    ...
                  end
                end

2010   8   28
title "            ",
                     :only => [:edit, :update]



2010   8   28
ApplicationController
                class ApplicationController < ...
                  attr_accessor :title


                 def self.title(name, options={})
                    before_lter(options) {|controller|
                                   controller.title = name}
                 end
                 .....
2010   8   28
English naming
                  problems

                validate ?
                validates ?
2010   8   28
2. Using Blocks




2010   8   28
examples : rake


                namespace :myapp do
                 task :my_rake_task do
                   # rake
                 end
                end

2010   8   28
Example : Rails Form
                       Helper

                form_for :book do |f|
                  f.text_eld :name
                  f.submit
                end


2010   8   28
Usages of block for DSL


                •   describe complex structure

                • get some tasks into a scope
2010   8   28
describe structure




2010   8   28
Example 3


                Write a game rule
                using playing cards



2010   8   28
Sevens


                game 'sevens' do |g|
                 g.use 53
                 g.deal 53, :to => :each_player
                 ...
                end


2010   8   28
Top
                You   Level
                              Game

                      game    use
                              deal


2010   8   28
class Game
                  def use (card_size, options ={})
                   ...
                  end
                  def deal (card_size, options ={})
                   ...
                  end
                end

2010   8   28
def game
                 g = Game.new
                 yield g
                end

2010   8   28
Top    Game
                You   Level




2010   8   28
Top        Game
                You      Level
                      game

                             Block


2010   8   28
Top        Game
                You      Level
                      game           new

                             Block


2010   8   28
Top        Game
                You      Level
                      game           new

                             Block


2010   8   28
Top        Game
                You      Level
                                           use
                      game           new
                                           deal

                             Block


2010   8   28
want the block
                parameter removed?




2010   8   28
without parameter g


                game 'sevens' do
                 use 53
                 deal 53, :to => :each_player
                 ...
                end


2010   8   28
use instance_exec



                def game(&block)
                 g = Game.new
                 g.instance_exec(&block)
                end


2010   8   28
can be changed slightly


                game 'sevens' do
                 uses 53
                 deals 53, :to => :each_player
                 ...
                end


2010   8   28
3. methods represent
                   special concepts




2010   8   28
methods
                that work as noun




2010   8   28
methods that represent
                     vocabularies
                 in another language




2010   8   28
books_url
                f.submit page.assert
                session   request
                current_user
2010   8   28
an extra DSL style




2010   8   28
4. Methods to make
                 codes like English




2010   8   28
should
                  it    years_since
                bytes
                    from_now
2010   8   28
Balance
                REALLY       Cosmos
                Useful ?     of OOP




2010   8   28
General Tips




2010   8   28
General Tips
                • provide default values for parameters
                • use a hash parameter
                • use symbols
                • naming methods nicely
2010   8   28
Summary




2010   8   28
DSL is
                not very special




2010   8   28
Implementation will
                improve with DSL ideas




2010   8   28
Have a try !




2010   8   28
The best way to learn
                  is reading codes




2010   8   28
Thank you !




2010   8   28

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (19)

کنترل اشیا با مغز
کنترل اشیا با مغزکنترل اشیا با مغز
کنترل اشیا با مغز
 
DSL in Clojure
DSL in ClojureDSL in Clojure
DSL in Clojure
 
پلتفرمهای نرم افزاری و سخت افزاری پیاده سازی راهکارهای اینترنت اشیاء
پلتفرمهای نرم افزاری و سخت افزاری پیاده سازی راهکارهای اینترنت اشیاءپلتفرمهای نرم افزاری و سخت افزاری پیاده سازی راهکارهای اینترنت اشیاء
پلتفرمهای نرم افزاری و سخت افزاری پیاده سازی راهکارهای اینترنت اشیاء
 
Cloud ofthings
Cloud ofthingsCloud ofthings
Cloud ofthings
 
اینترنت اشیا در 10 دقیقه
اینترنت اشیا در 10 دقیقهاینترنت اشیا در 10 دقیقه
اینترنت اشیا در 10 دقیقه
 
Terms of endearment - the ElasticSearch Query DSL explained
Terms of endearment - the ElasticSearch Query DSL explainedTerms of endearment - the ElasticSearch Query DSL explained
Terms of endearment - the ElasticSearch Query DSL explained
 
مروی بر استارترکیتها و پلتفرمهای اینترنت اشیاء
مروی بر استارترکیتها و پلتفرمهای اینترنت اشیاءمروی بر استارترکیتها و پلتفرمهای اینترنت اشیاء
مروی بر استارترکیتها و پلتفرمهای اینترنت اشیاء
 
داده های عظیم چگونه دنیا را تغییر خواهند داد
داده های عظیم چگونه دنیا را تغییر خواهند داد داده های عظیم چگونه دنیا را تغییر خواهند داد
داده های عظیم چگونه دنیا را تغییر خواهند داد
 
کاربردهای اینترنت اشیاء در حوزه سازمانی و صنعت
کاربردهای اینترنت اشیاء در حوزه سازمانی و صنعت کاربردهای اینترنت اشیاء در حوزه سازمانی و صنعت
کاربردهای اینترنت اشیاء در حوزه سازمانی و صنعت
 
Practical Groovy DSL
Practical Groovy DSLPractical Groovy DSL
Practical Groovy DSL
 
طراحی و پیاده سازی سیستم کنترل هوشمند آبیاری گیاهان و باغچه (IoT)
 طراحی و پیاده سازی سیستم کنترل هوشمند آبیاری گیاهان و باغچه (IoT)  طراحی و پیاده سازی سیستم کنترل هوشمند آبیاری گیاهان و باغچه (IoT)
طراحی و پیاده سازی سیستم کنترل هوشمند آبیاری گیاهان و باغچه (IoT)
 
Configuration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL PluginConfiguration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL Plugin
 
Internet of Things Security Challlenges
Internet of Things Security ChalllengesInternet of Things Security Challlenges
Internet of Things Security Challlenges
 
IOT security
IOT securityIOT security
IOT security
 
ISDN & DSL
ISDN & DSLISDN & DSL
ISDN & DSL
 
DSL, Page Object and Selenium – a way to reliable functional tests
DSL, Page Object and Selenium – a way to reliable functional testsDSL, Page Object and Selenium – a way to reliable functional tests
DSL, Page Object and Selenium – a way to reliable functional tests
 
Internet of Things
Internet of ThingsInternet of Things
Internet of Things
 
Global In-house Site contribution and value analysis
Global In-house Site contribution and value analysisGlobal In-house Site contribution and value analysis
Global In-house Site contribution and value analysis
 
Zinnov Zones for IoT Services 2017
Zinnov Zones for IoT Services 2017Zinnov Zones for IoT Services 2017
Zinnov Zones for IoT Services 2017
 

Mehr von Yasuko Ohba

Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Yasuko Ohba
 
QCon2009 Tokyo - Ruby on Railsで変わるエンタープライズ開発の現場
QCon2009 Tokyo - Ruby on Railsで変わるエンタープライズ開発の現場QCon2009 Tokyo - Ruby on Railsで変わるエンタープライズ開発の現場
QCon2009 Tokyo - Ruby on Railsで変わるエンタープライズ開発の現場
Yasuko Ohba
 

Mehr von Yasuko Ohba (20)

Rubyによる開発プロジェクトをうまく回すには(2)
Rubyによる開発プロジェクトをうまく回すには(2)Rubyによる開発プロジェクトをうまく回すには(2)
Rubyによる開発プロジェクトをうまく回すには(2)
 
Rubyによる開発プロジェクトをうまく回すには(1)
Rubyによる開発プロジェクトをうまく回すには(1)Rubyによる開発プロジェクトをうまく回すには(1)
Rubyによる開発プロジェクトをうまく回すには(1)
 
TECH LAB PAAK 2015/06/24 Team Development
TECH LAB PAAK 2015/06/24 Team DevelopmentTECH LAB PAAK 2015/06/24 Team Development
TECH LAB PAAK 2015/06/24 Team Development
 
女性IT技術者と働き方 情報処理学会77
女性IT技術者と働き方 情報処理学会77女性IT技術者と働き方 情報処理学会77
女性IT技術者と働き方 情報処理学会77
 
Girl, Geek and Company - Tokyo Girl Geek Dinners #5 2013/7/5
Girl, Geek and Company - Tokyo Girl Geek Dinners #5 2013/7/5Girl, Geek and Company - Tokyo Girl Geek Dinners #5 2013/7/5
Girl, Geek and Company - Tokyo Girl Geek Dinners #5 2013/7/5
 
世界を描く Drawing the world
世界を描く Drawing the world世界を描く Drawing the world
世界を描く Drawing the world
 
Sendai ruby-02
Sendai ruby-02Sendai ruby-02
Sendai ruby-02
 
Good Names in Right Places on Rails
Good Names in Right Places on RailsGood Names in Right Places on Rails
Good Names in Right Places on Rails
 
ごきげんRails
ごきげんRailsごきげんRails
ごきげんRails
 
名前のつけ方
名前のつけ方名前のつけ方
名前のつけ方
 
Shimane2010
Shimane2010Shimane2010
Shimane2010
 
Smell in Rails Apps (in Sapporo RubyKaigi03)
Smell in Rails Apps (in Sapporo RubyKaigi03)Smell in Rails Apps (in Sapporo RubyKaigi03)
Smell in Rails Apps (in Sapporo RubyKaigi03)
 
Sub Resources Rails Plug-in
Sub Resources Rails Plug-inSub Resources Rails Plug-in
Sub Resources Rails Plug-in
 
More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02
More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02
More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02
 
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
 
QCon2009 Tokyo - Ruby on Railsで変わるエンタープライズ開発の現場
QCon2009 Tokyo - Ruby on Railsで変わるエンタープライズ開発の現場QCon2009 Tokyo - Ruby on Railsで変わるエンタープライズ開発の現場
QCon2009 Tokyo - Ruby on Railsで変わるエンタープライズ開発の現場
 
Raspbilly
RaspbillyRaspbilly
Raspbilly
 
テスト大嫌いっ娘のRSpec
テスト大嫌いっ娘のRSpecテスト大嫌いっ娘のRSpec
テスト大嫌いっ娘のRSpec
 
Shimane2008
Shimane2008Shimane2008
Shimane2008
 
Ruby on Rails 入門
Ruby on Rails 入門Ruby on Rails 入門
Ruby on Rails 入門
 

KĂźrzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Enterprise Knowledge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

KĂźrzlich hochgeladen (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

The Basis of Making DSL with Ruby