2. DSL Powered by Rabbit 2.1.2
Who am I ?
Yukio Goto
favorite
Ruby, emacs, zsh, tennis✓
✓
work
Rakuten Asia Pte. Ltd.✓
As senior application engineer✓
✓
✓
24. DSL Powered by Rabbit 2.1.2
Benefit of making DSL
DSL makes your Application
easy to write✓
easy to read✓
easy to use✓
✓
25. DSL Powered by Rabbit 2.1.2
theme
Configuration file using DSL
26. DSL Powered by Rabbit 2.1.2
config version 1
# singapore-ruby-group.conf
meetup do |conf|
conf.group = 'Singapore-Ruby-Group'
conf.organizer = 'Winston Teo'
conf.sponsors = ['Engine Yard',
'Silicon Straits',
'Plug-In@Blk71']
end
27. DSL Powered by Rabbit 2.1.2
how to use
mt = Meetup.load('singapore-ruby-group.conf')
puts mt.group
# => Singapore-Ruby-Group
puts mt.organizer
# => Winston Teo
puts mt.sponsors.join(', ')
# => Engine Yard, Silicon Straits, Plug-In@Blk71
28. DSL Powered by Rabbit 2.1.2
implementation
# Sample class to load configuration
class Meetup
attr_accessor :group, :organizer, :sponsors
def self.load(path)
fail "config file not found: #{path}" unless File.exist?(path)
mt = Meetup.new
File.open(path, 'r') do |file|
mt.instance_eval(File.read(path), path)
end
mt
end
private
def meetup
yield self
end
end
29. DSL Powered by Rabbit 2.1.2
key point
def self.load
# ...
File.open(path, 'r') do |file|
mt.instance_eval(File.read(path), path)
end
# ...
end
def meetup
yield self
end
30. DSL Powered by Rabbit 2.1.2
first point
pass string to 'instance_eval'
def self.load
# ...
File.open(path, 'r') do |file|
mt.instance_eval(File.read(path), path)
end
# ...
end
31. DSL Powered by Rabbit 2.1.2
instance_eval
"instance_eval"
32. DSL Powered by Rabbit 2.1.2
instance_eval
interpretes
String
51. DSL Powered by Rabbit 2.1.2
load
class Meetup
attr_accessor :group, :organizer, :sponsors
def self.load(path)
mt = new
File.open(path, 'r') do |file|
loader = MeetupLoader.new(mt)
loader.instance_eval(file.read, path)
end
mt
end
end
52. DSL Powered by Rabbit 2.1.2
make support class
make support class
MeetupLoader
53. DSL Powered by Rabbit 2.1.2
make support class
define setter
method
54. DSL Powered by Rabbit 2.1.2
make support class
not use '='
56. DSL Powered by Rabbit 2.1.2
make support class
load function
(meetup) only
57. DSL Powered by Rabbit 2.1.2
make support class (setter)
class MeetupLoader
def initialize(mt)
@meetup = mt
end
# ...
def group(g)
@meetup.group = g
end
def organizer(o)
@meetup.organizer = o
end
# ...
end
58. DSL Powered by Rabbit 2.1.2
instance_eval again
class MeetupLoader
# ...
def meetup(&block)
instance_eval(&block)
end
# ...
end
59. DSL Powered by Rabbit 2.1.2
The point of black magic
instance_eval
again
60. DSL Powered by Rabbit 2.1.2
instance_eval for block
instance_eval
with block
64. DSL Powered by Rabbit 2.1.2
original code
class Meetup
attr_accessor :group, :organizer, :sponsors
def self.load(path)
mt = new
File.open(path, 'r') do |file|
loader = MeetupLoader.new(mt)
loader.instance_eval(file.read, path)
end
mt
end
end
65. DSL Powered by Rabbit 2.1.2
expand instance_eval virtually 1
def self.load(path)
mt = new
File.open(path, 'r') do |file|
loader = MeetupLoader.new(mt)
# loader.instance_eval(file.read, path)
loader.meetup {
group 'Singapore-Ruby-Group'
organizer 'Winston Teo'
# ...
}
end
#...
66. DSL Powered by Rabbit 2.1.2
instance_eval with block
class MeetupLoader
# ...
def meetup(&block)
instance_eval(&block)
end
# ...
end
81. DSL Powered by Rabbit 2.1.2
take a way
You can make DSL✓
instance_eval is interesting✓
82. DSL Powered by Rabbit 2.1.2
Hiring
Rakuten Asia Pte. Ltd.
wants to hire
Server side Application
Engineer (Ruby, java)
✓
iOS, Android Application
Engineer
✓
✓
83. DSL Powered by Rabbit 2.1.2
Any question ?
Any question ?
84. DSL Powered by Rabbit 2.1.2
Thank you
Thank you
for listening