Skip to content

Instantly share code, notes, and snippets.

@wavebeem
Created July 8, 2016 02:51
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 wavebeem/c5c0c0e8e6ccad595665060f7120ffba to your computer and use it in GitHub Desktop.
Save wavebeem/c5c0c0e8e6ccad595665060f7120ffba to your computer and use it in GitHub Desktop.
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