Created
December 10, 2015 17:10
-
-
Save JerrySeto/93bf72ec8ee83ecbf1cf to your computer and use it in GitHub Desktop.
Interface for repeating
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
| 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 | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can do something like: