Ticket #7184: docs_for_delegate.diff
| File docs_for_delegate.diff, 1.3 kB (added by jeremymcanally, 1 year ago) |
|---|
-
activesupport/lib/active_support/core_ext/module/delegation.rb
old new 5 5 # or string). At least one method and the :to option are required. 6 6 # 7 7 # Delegation is particularly useful with Active Record associations: 8 # 8 9 # class Greeter < ActiveRecord::Base 9 10 # def hello() "hello" end 10 11 # def goodbye() "goodbye" end … … 25 26 # end 26 27 # 27 28 # 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 # 28 48 def delegate(*methods) 29 49 options = methods.pop 30 50 unless options.is_a?(Hash) && to = options[:to]