Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7184: docs_for_delegate.diff

File docs_for_delegate.diff, 1.3 kB (added by jeremymcanally, 1 year ago)

Updated for edge

  • activesupport/lib/active_support/core_ext/module/delegation.rb

    old new  
    55  # or string).  At least one method and the :to option are required. 
    66  # 
    77  # Delegation is particularly useful with Active Record associations: 
     8  # 
    89  #   class Greeter < ActiveRecord::Base 
    910  #     def hello()   "hello"   end 
    1011  #     def goodbye() "goodbye" end 
     
    2526  #   end 
    2627  # 
    2728  #   Foo.new.goodbye # => "goodbye" 
     29  # 
     30  # Methods can be delegated to instance variables, class variables, or constants 
     31  # by providing the variable as a symbol: 
     32  #   class Foo 
     33  #     CONSTANT_ARRAY = [0,1,2,3] 
     34  #     @@class_array  = [4,5,6,7] 
     35  #      
     36  #     def initialize 
     37  #       @instance_array = [8,9,10,11] 
     38  #     end 
     39  #     delegate :sum, :to => :CONSTANT_ARRAY 
     40  #     delegate :min, :to => :@@class_array 
     41  #     delegate :max, :to => :@instance_array 
     42  #   end 
     43  # 
     44  #   Foo.new.sum # => 6 
     45  #   Foo.new.min # => 4 
     46  #   Foo.new.max # => 11 
     47  # 
    2848  def delegate(*methods) 
    2949    options = methods.pop 
    3050    unless options.is_a?(Hash) && to = options[:to]