Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module MyDecorators
def limit(max, name)
tries = 0
m = instance_method(name)
define_method(name) do |*args|
tries += 1
if tries <= max
m.bind(self).call(*args)
else
raise "LIMITER ENGAGED"
end
end
end
def only_once(name)
limit(1, name)
end
end
class Person
extend MyDecorators
limit 10, def talk
puts "Hi there!"
end
only_once def scream
puts "HIIIIIIIIIIIIIIIIII!"
end
end
p = Person.new
20.times { p.talk rescue nil }
20.times { p.scream rescue nil }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment