開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- ruby 2.4 (プログラミング言語)
- 初めてのRuby(その他の参考書籍)
メタプログラミングRuby 第2版(Paolo Perrotta (著)、角 征典 (翻訳)、オライリージャパン)の1部(メタプログラミング Ruby)、4章(水曜日: メソッド)、4.2(クイズ: より良い DSL)を取り組んでみる。
コード(Emacs)
#!/usr/bin/env ruby2.4
# -*- coding: utf-8 -*-
def setup(&block)
@setups << block
end
def event(description, &block)
@events << {:description => description, :condition => block}
end
@setups = []
@events = []
load "events.rb"
@events.each do |event|
@setups.each do |setup|
setup.call
end
if event[:condition].call
puts "ALERT: #{event[:description]}"
end
end
入出力結果(Terminal)
$ ./redflag.rb Setting up sky Setting up mountains ALERT: the sky is falling Setting up sky Setting up mountains ALERT: it's getting closer Setting up sky Setting up mountains $
0 コメント:
コメントを投稿