Skip to content
This repository
Browse code

Document that the delegate method can delegate to things other than j…

…ust methods. Closes #7184 [dcmanges, jeremymcanally]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8311 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit cdc4e93dfa46032b136d67bdb71b1e1f3d99cd1a 1 parent b9e0b72
authored December 05, 2007
20  activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -5,6 +5,7 @@ class Module
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,6 +26,25 @@ class Module
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]

0 notes on commit cdc4e93

Please sign in to comment.
Something went wrong with that request. Please try again.