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