From 42f58bc56e9eee352b03c2f79b9096f7ac9b41b1 Mon Sep 17 00:00:00 2001 From: Sandor Szuecs Date: Sat, 21 May 2011 12:19:14 +0200 Subject: [PATCH] forwardable doc def_delegator with example --- lib/forwardable.rb | 19 +++++++++++++++++++ 1 files changed, 19 insertions(+), 0 deletions(-) diff --git a/lib/forwardable.rb b/lib/forwardable.rb index b43d00f..2b71b90 100644 --- a/lib/forwardable.rb +++ b/lib/forwardable.rb @@ -175,6 +175,25 @@ module Forwardable end end + # Define +method+ as delegator instance method with an optional + # alias name +ali+. Method calls to +ali+ will be delegated to + # +accessor.method+. + # + # class MyQueue + # extend Forwardable + # attr_reader :queue + # def initialize + # @queue = [] + # end + # + # def_delegator :@queue, :push, :mypush + # end + # + # q = MyQueue.new + # q.mypush 42 + # q.queue #=> [42] + # q.push 23 #=> NoMethodError + # def def_instance_delegator(accessor, method, ali = method) line_no = __LINE__; str = %{ def #{ali}(*args, &block) -- 1.7.5.1