Last active
November 16, 2017 00:13
-
-
Save stevenbarragan/7a3734914c78f8d7e80eea5084b91ad5 to your computer and use it in GitHub Desktop.
This file contains 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
class Throttler | |
def initialize(time_limit, &block) | |
@block = block | |
@created_at = Time.now | |
@time_limit = time_limit | |
@block.call() | |
end | |
def call | |
@block.call() if call_now? | |
end | |
def call_now? | |
Time.now > @created_at + @time_limit | |
end | |
end | |
function = Throttler.new(1) do | |
puts "Hello World" | |
end | |
function.call() | |
function.call() | |
function.call() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment