Skip to content

Instantly share code, notes, and snippets.

@krasio
Created October 12, 2011 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krasio/1280872 to your computer and use it in GitHub Desktop.
Save krasio/1280872 to your computer and use it in GitHub Desktop.
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