Document that the delegate method can delegate to things other than just methods. Closes #7184 [dcmanges, jeremymcanally]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8311 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Marcel Molina
2007-12-05 21:45:35 +00:00
parent b9e0b72795
commit cdc4e93dfa

View File

@@ -5,6 +5,7 @@ class Module
# or string). At least one method and the :to option are required.
#
# Delegation is particularly useful with Active Record associations:
#
# class Greeter < ActiveRecord::Base
# def hello() "hello" end
# def goodbye() "goodbye" end
@@ -25,6 +26,25 @@ class Module
# end
#
# Foo.new.goodbye # => "goodbye"
#
# Methods can be delegated to instance variables, class variables, or constants
# by providing the variable as a symbol:
# class Foo
# CONSTANT_ARRAY = [0,1,2,3]
# @@class_array = [4,5,6,7]
#
# def initialize
# @instance_array = [8,9,10,11]
# end
# delegate :sum, :to => :CONSTANT_ARRAY
# delegate :min, :to => :@@class_array
# delegate :max, :to => :@instance_array
# end
#
# Foo.new.sum # => 6
# Foo.new.min # => 4
# Foo.new.max # => 11
#
def delegate(*methods)
options = methods.pop
unless options.is_a?(Hash) && to = options[:to]