2008-01-25から1日間の記事一覧

Template Method その5

Template Method その3のモジュールを使えば、 Enumerableモジュールと同じように、こんな事もできますね。 obj = Object.new def obj.open puts "open!" end def obj.close puts "close!" end def obj.temp [:open, :close] end obj.extend(TemplateMethod:…

Template Method その4

module TemplateMethod2 class Child def open print "<" end def disp(&block) open block.call close end def close puts ">" end end end TemplateMethod2::Child.new.disp { print "H" * 5 } 初めと意味が変わってしまいましたが、 これもTemplate Metho…

Template Method その3

module TemplateMethod module Display def disp temp.each { |sym| __send__(sym) } end end class Parent include Display def temp a = [] a << :open a << [:_print] * 5 a << :close a.flatten end end class Child < Parent def initialize(str) @str …

Template Method その2

module TemplateMethod class Display def self.disp(procs) procs[:open].call procs[:_print].call procs[:close].call end end end def char_procs(str) { :open => proc { print "<" }, :_print => proc { print str * 5 }, :close => proc { puts ">" }…