mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	Document and add spec for delegating to constants in Forwardable
Fixes [Bug #13142]
This commit is contained in:
		
							parent
							
								
									21ce8b3298
								
							
						
					
					
						commit
						4b7d7d007f
					
				
					 2 changed files with 19 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -122,7 +122,8 @@ module Forwardable
 | 
			
		|||
  end
 | 
			
		||||
 | 
			
		||||
  # Takes a hash as its argument.  The key is a symbol or an array of
 | 
			
		||||
  # symbols.  These symbols correspond to method names.  The value is
 | 
			
		||||
  # symbols.  These symbols correspond to method names, instance variable
 | 
			
		||||
  # names, or constant names (see def_delegator).  The value is
 | 
			
		||||
  # the accessor to which the methods will be delegated.
 | 
			
		||||
  #
 | 
			
		||||
  # :call-seq:
 | 
			
		||||
| 
						 | 
				
			
			@ -160,9 +161,12 @@ module Forwardable
 | 
			
		|||
 | 
			
		||||
  # Define +method+ as delegator instance method with an optional
 | 
			
		||||
  # alias name +ali+. Method calls to +ali+ will be delegated to
 | 
			
		||||
  # +accessor.method+.
 | 
			
		||||
  # +accessor.method+.  +accessor+ should be a method name, instance
 | 
			
		||||
  # variable name, or constant name.  Use the full path to the
 | 
			
		||||
  # constant if providing the constant name.
 | 
			
		||||
  #
 | 
			
		||||
  #   class MyQueue
 | 
			
		||||
  #     CONST = 1
 | 
			
		||||
  #     extend Forwardable
 | 
			
		||||
  #     attr_reader :queue
 | 
			
		||||
  #     def initialize
 | 
			
		||||
| 
						 | 
				
			
			@ -170,12 +174,14 @@ module Forwardable
 | 
			
		|||
  #     end
 | 
			
		||||
  #
 | 
			
		||||
  #     def_delegator :@queue, :push, :mypush
 | 
			
		||||
  #     def_delegator 'MyQueue::CONST', :to_i
 | 
			
		||||
  #   end
 | 
			
		||||
  #
 | 
			
		||||
  #   q = MyQueue.new
 | 
			
		||||
  #   q.mypush 42
 | 
			
		||||
  #   q.queue    #=> [42]
 | 
			
		||||
  #   q.push 23  #=> NoMethodError
 | 
			
		||||
  #   q.to_i     #=> 1
 | 
			
		||||
  #
 | 
			
		||||
  def def_instance_delegator(accessor, method, ali = method)
 | 
			
		||||
    gen = Forwardable._delegator_method(self, accessor, method, ali)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,7 @@ require 'test/unit'
 | 
			
		|||
require 'forwardable'
 | 
			
		||||
 | 
			
		||||
class TestForwardable < Test::Unit::TestCase
 | 
			
		||||
  INTEGER = 42
 | 
			
		||||
  RECEIVER = BasicObject.new
 | 
			
		||||
  RETURNED1 = BasicObject.new
 | 
			
		||||
  RETURNED2 = BasicObject.new
 | 
			
		||||
| 
						 | 
				
			
			@ -27,6 +28,16 @@ class TestForwardable < Test::Unit::TestCase
 | 
			
		|||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_def_instance_delegator_constant
 | 
			
		||||
    %i[def_delegator def_instance_delegator].each do |m|
 | 
			
		||||
      cls = forwardable_class do
 | 
			
		||||
        __send__ m, 'TestForwardable::INTEGER', :to_i
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      assert_equal 42, cls.new.to_i
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_def_instance_delegator_using_args_method_as_receiver
 | 
			
		||||
    %i[def_delegator def_instance_delegator].each do |m|
 | 
			
		||||
      cls = forwardable_class(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue