Skip to content

Instantly share code, notes, and snippets.

@andycamp
Created April 6, 2016 13:04
Show Gist options
  • Save andycamp/143f15f5c4ab28c1863758df8311c6c1 to your computer and use it in GitHub Desktop.
Save andycamp/143f15f5c4ab28c1863758df8311c6c1 to your computer and use it in GitHub Desktop.
Builds up object instances with blocks in a module (inspired by rack-attack)
module Prank
@@jokes = []
class Joke
def initialize(name, options, block)
@name = name
@options = options
@block = block
self
end
def tell
puts "Telling: #{@name.to_s}"
puts "Options: #{@options.to_s}"
@block.call if @block.respond_to?(:call)
end
end
def self.joke(name, options = {}, &block)
self.jokes[name] = Joke.new(name, options, block)
end
def self.jokes
@jokes ||= {}
end
def self.tell_jokes
@jokes.each do |name, joke|
puts "telling #{name}"
joke.tell
end
end
joke("Knock-knock", :this => "haha") do
puts "Knock-knock"
end
joke("Limeric", :type => "limirick") do
puts "There once was a man"
end
end
Prank.tell_jokes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment