http://www.iteye.com/topic/370266
中写到:
2.Builder
Ruby代码
- # 工头
- class Director
- def build_with builder
- acc = ''
- [:header, :body, :footer].each do |m|
- acc += builder.__send__ m if builder.respond_to? m
- end
- acc
- end
- end
- # 工人
- class HTMLBuilder
- def header; 'html builder';end
- def body; 'html builder' ;end
- def footer; '' ;end
- end
- class XMLBuilder
- def header; '<?xml version="1.0" charset="utf-8">';end
- def body; 'xml builder' ;end
- end
- d = Director.new
- puts(d.build_with HTMLBuilder.new)
- puts(d.build_with XMLBuilder.new)
- end
这个模式中,我要给header(), body() 带参数怎么写Director类?
[b]问题补充:[/b]
def build_with buildercolor=red [/color]
acc = ''
[:header, :body, :footer].each do |m|
acc += builder.__send__ m,*args if builder.respond_to? m
end
acc
end
语法都不正确?
你跑过吗?
[img]/upload/attachment/112237/711aba2b-da48-3577-88d3-f926fb02600a.jpg[/img]
[b]问题补充:[/b]
您没有仔细看代码?
build_with( builder(*args) )
build_with 是方法。
builder 是一个类对象