Skip to content

Instantly share code, notes, and snippets.

@JerrySeto
Created December 10, 2015 17:10
Show Gist options
  • Select an option

  • Save JerrySeto/93bf72ec8ee83ecbf1cf to your computer and use it in GitHub Desktop.

Select an option

Save JerrySeto/93bf72ec8ee83ecbf1cf to your computer and use it in GitHub Desktop.
Interface for repeating
require 'pry'
module Repeats
def n_times(n)
Repeats::Repeater.new(self, n)
end
class Repeater
def initialize(parent, times)
@parent = parent
@times = times
end
def method_missing(method, *args)
@times.times do
@parent.send method.to_sym, *args
end
end
end
end
class Dog
include Repeats
def bark
puts 'woof'
end
end
@JerrySeto
Copy link
Author

You can do something like:

Dog.new.n_times(3).bark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment