Created
October 12, 2011 10:34
-
-
Save krasio/1280872 to your computer and use it in GitHub Desktop.
Require No Interface - http://practicingruby.com/articles/shared/ozkzbsdmagcm
This file contains 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
module Inspector | |
def self.analyze(data) | |
registered_plugins.each do |name, block| | |
puts "Report: #{name}" | |
block.call(data) | |
end | |
end | |
def self.registered_plugins | |
@registered_plugins ||= {} | |
end | |
def self.report(name, &block) | |
registered_plugins[name] = block | |
end | |
end | |
module WordLengthPlugin | |
def self.analyze(data) | |
longest = data.split(/ /).map { |e| e.length }.max | |
puts "Longest word contained #{longest} characters" | |
end | |
end | |
# delegates to the WordLengthPlugin module | |
Inspector.report("Word Length") do |data| | |
WordLengthPlugin.analyze(data) | |
end | |
# implements the report as an inline function | |
Inspector.report("Word Count") do |data| | |
word_count = data.split(/ /).length | |
puts "Content contained #{word_count} words" | |
end | |
Inspector.analyze("This is a test of the watcher plugins") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment