Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
Marcel Molina authored
Showing with 20 additions and 0 deletions.
  1. +20 −0 activesupport/lib/active_support/core_ext/module/delegation.rb
View
20 activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -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]
Please sign in to comment.
Something went wrong with that request. Please try again.