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

Ticket #7184: doc_for_delegate.diff

File doc_for_delegate.diff, 1.1 kB (added by dcmanges, 2 years ago)
  • lib/active_support/core_ext/module/delegation.rb

    old new  
    2020  # 
    2121  # Multiple delegates to the same target are allowed: 
    2222  #   class Foo < ActiveRecord::Base 
     23  #     belongs_to :greeter 
    2324  #     delegate :hello, :goodbye, :to => :greeter 
    2425  #   end 
    2526  # 
    2627  #   Foo.new.goodbye # => "goodbye" 
     28  # 
     29  # Methods can be delegated to instance variables, class variables, or constants 
     30  # by providing the variable as a symbol: 
     31  #   class Foo 
     32  #     CONSTANT_ARRAY = [0,1,2,3] 
     33  #     @@class_array  = [4,5,6,7] 
     34  #      
     35  #     def initialize 
     36  #       @instance_array = [8,9,10,11] 
     37  #     end 
     38  #     delegate :sum, :to => :CONSTANT_ARRAY 
     39  #     delegate :min, :to => :@@class_array 
     40  #     delegate :max, :to => :@instance_array 
     41  #   end 
     42  # 
     43  #   Foo.new.sum # => 6 
     44  #   Foo.new.min # => 4 
     45  #   Foo.new.max # => 11 
     46  # 
    2747  def delegate(*methods) 
    2848    options = methods.pop 
    2949    unless options.is_a?(Hash) && to = options[:to]