Skip to content

Instantly share code, notes, and snippets.

@monzou
Created June 12, 2011 12:01
Show Gist options
  • Save monzou/1021483 to your computer and use it in GitHub Desktop.
Save monzou/1021483 to your computer and use it in GitHub Desktop.
Strategy (lambda)
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