Slide 36
Slide 36 text
MergeConf’22
Router v2: instance_eval
36
* https://gist.github.com/DmitryTsepelev/ab1797e7d00b484973615ba1782e0e36#
fi
le-05_builder_instance_eval-rb
class Router
NOT_FOUND = 'NOT_FOUND'
def self.draw(&block)
new.tap { |router| router.instance_eval(&block) }
end
def route(method_and_path)
method, path = method_and_path.split(' ')
routes[[method.downcase, path]]
| |
NOT_FOUND
end
def get(path,
* *
args)
routes[['get', path]] = args
end
def post(path,
* *
args)
routes[['post', path]] = args
end
def routes
@routes
| |
= {}
end
end
блок будет выполнен в
контексте инстанса Router
все эти методы
доступны внутри блока,
переданного instance_eval