Created
April 6, 2016 13:04
-
-
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)
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
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