短絡的に作るRuby DSL 公開版
description
Transcript of 短絡的に作るRuby DSL 公開版
@bash0C7 こしば としあき
日本Ruby会議2010実行委員
IDE派Ruby,PHPユーザ
モバイルショッピング事業室
11/20(土)PMRailsDevCon
2/4(金)夜東京Ruby会議05
(予定)
お品書き
menu
Ruby
PowerPoint
DSL
DSL
ドメイン特化言語
Domain Specific Language
汎化
特化
汎化
Java
Perl
Ruby
COBOL
Fortran
PHP
特化
JCL
make
shell
rake
rspec
sinatra
外部
内部
独自文法
汎用言語の文法
外部DSL内部DSL
外部DSL
http://www.coplus.jp/download/m/GC88-6578-09(c8865789).pdf
//INIT JOB 'accounting_info',MSGLEVEL=1
//JESDS OUTPUT JESDS=ALL,OUTDISP=(PURGE,WRITE)
//INIT EXEC INIT
//DD1 DD DSN=SYSTEM.ACCOUNT.DATA,DISP=SHR
//*
By TreyGeek (talk) 18:52, 16 May 2008 (UTC).TreyGeek at en.wikipedia [Public domain], from Wikimedia Commons
内部DSL
rake
rspec
sinatra
rake
rule '.o' => '.c' do |t|
sh "cc -c -o #{t.name} #{t.source}"
end
file 'hello' => ['hello.o'] do
sh 'cc -o hello hello.o'
end
http://ja.wikipedia.org/wiki/Rake
rspec
describe Array, "when empty" do
before do
@empty_array = []
end
it "should be empty" do
@empty_array.should be_empty
endhttp://jp.rubyist.net/magazine/?0021-Rspec
いつ作る?
どう作る?
プレゼン準備
Ruby
DSL
PowerPoint
DSL
初期スケッチ
presentation do
slides do
slide {……}
slide {……}
end
end
実装したDSL
theme "theme.potx"
title "短絡的に作るRubyDSL"
text "Domail Specitific Language"
text "特定の領域にのみ特化"
division "DSL世界の2軸"
section "Generic⇔Specitific"
ロード
評価
実行
>pptgen.rb [DSL file]
PresentationDSLProcesser.new(ARGF.read)
def initialize(dsl_string)
@app = WIN32OLE.new('Powerpoint.application')
@presentation = @app.Presentations.Add
eval(dsl_string)
@presentation.saveas('aaa.pptx')
@presentation.close
@app.quit
end
eval(dsl_string)
動けばええねん!
theme "theme.potx"
title "短絡的に作るRubyDSL"
text "Domail Specitific Language"
text "特定の領域にのみ特化"
division "DSL世界の2軸"
section "Generic⇔Specitific"
スライド追加
スライド枚数++
レイアウト設定
文字列を設定
title "短絡的に作るRubyDSL"
title "短絡的に作るRubyDSL" → title(param)
def title(param)
add_slide(1)
set_title(param)
end
title(param) → add_slide(1)
def add_slide(layout)
@current_slide =
@presentation.Slides.AddSlide(@presentation.Slides.Count + 1,@presentation.SlideMaster.
CustomLayouts(1))
@current_slide.layout = layout
end
title "短絡的に作るRubyDSL" → title(param)
def title(param)
add_slide(1)
set_title(param)
end
title(param) → set_title(param)
def set_title(param)
@current_slide.Shapes.Placeholders(1).TextFrame.TextRange.Text = param
end
スライド追加
スライド枚数++
レイアウト設定
文字列を設定
title "短絡的に作るRubyDSL"
一言title(文字列)
PowerPoint
Win32OLE標準添付ライブラリ
WIN32OLE.new('Powerpoint.application')
PowerPointをRubyから
Interaction.CreateObject("Powerpoint.application")
PowerPointをxxxから
CreateObject("Powerpoint.application")
PowerPointをVBA/VBSから
VBA/VBS→pptRuby→ppt
def initialize(presen)
@app = WIN32OLE.new('Powerpoint.application')
@presentation = @app.Presentations.Add
eval(presen)
@presentation.saveas('aaa.pptx')
@presentation.close
@app.quit
end
VBA≒Office付属VB
VBAライブラリ
Interactionモジュール
CreateObject関数
Ruby
標準添付ライブラリ
Win32OLEクラス
まとめ
Rubyの文法
特定用途特化
Ruby DSL
title "短絡的に作るRubyDSL"
欲しいものを端的に表現
いつ作る?
設計書生成
ビジネス文章生成
テストケース生成
ソースコード生成
どう作る?
eval(dsl_string)
動けばええねん!
Win32OLE
erb
他色々!
Happy DSLing!
ご清聴ありがとうございました
おまけ
VBA DSL
preview版
DSLクラス
Public Sub text(param As String)
collection.Add "テキスト:" & param
End Sub
Public Sub title(param As String)
collection.Add "タイトル:" & param
End Sub
解釈・実行
Dim dsl As New dsl
Dim r As Range
For Each r In ActiveSheet.UsedRange.Rows
CallByName dsl, r.Columns(2),
VbMethod, r.Columns(3)
Next r
dsl.disp
PHP DSL
no sample
one more thing...
即興DSL
方眼紙ExcelDSL