Last active
July 8, 2016 14:38
-
-
Save aoitaku/6dbf15f5be5fe3524a6bb03a5ba24fb5 to your computer and use it in GitHub Desktop.
forwardable を拡張する
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 'forwardable' | |
module Forwardable | |
def delegate_to(accessor, *methods, **method_hash) | |
methods.each do|method| | |
def_instance_delegator(accessor, method) | |
end | |
method_hash.each do|method, ali| | |
def_instance_delegator(accessor, method, ali) | |
end | |
end | |
end | |
class Queue | |
extend Forwardable | |
delegate_to :@array, | |
:each, | |
:push => :enq, | |
:shift => :deq | |
def initialize | |
@array = [] | |
end | |
end | |
q = Queue.new | |
q.enq 1 | |
q.enq 2 | |
q.enq 3 | |
q.enq 4 | |
q.enq 5 | |
while n = q.deq | |
p n | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment