Created
June 12, 2011 12:01
-
-
Save monzou/1021483 to your computer and use it in GitHub Desktop.
Strategy (lambda)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Report | |
attr_accessor :title, :text, :formatter | |
def initialize(&formatter) | |
@title = "No Title" | |
@text = "Not Found" | |
@formatter = formatter | |
end | |
def output_report | |
@formatter.call(self) | |
end | |
end | |
HTML_FORMATTER = lambda do |context| | |
puts("<html>") | |
puts("<body>") | |
puts("<h1>#{context.title}</h1>") | |
puts("<p>#{context.text}}</h1>") | |
puts("</body>") | |
puts("</html>") | |
end | |
report = Report.new(&HTML_FORMATTER) | |
report.title = "Hello" | |
report.text = "Hello, Strategy." | |
puts report.output_report | |
puts Report.new { |context| puts "#{context.title} / #{context.text}" }.output_report |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment