aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Schierbeck <daniel.schierbeck@gmail.com>2011-09-01 15:23:32 +0200
committerDaniel Schierbeck <dasch@zendesk.com>2012-04-12 13:47:52 +0200
commit3b1537a7d1c19d7f4ab6ae1a405ec4c2c186a55c (patch)
tree256e8b32c4e29468c15dd0e654f46250d53323f5
parent2310db373ba915e77fdc1dcd780068ad98d242cd (diff)
downloadrails-3b1537a7d1c19d7f4ab6ae1a405ec4c2c186a55c.tar.gz
rails-3b1537a7d1c19d7f4ab6ae1a405ec4c2c186a55c.tar.bz2
rails-3b1537a7d1c19d7f4ab6ae1a405ec4c2c186a55c.zip
Document the changes to delegate in the guides
-rw-r--r--guides/source/active_support_core_extensions.textile20
1 files changed, 20 insertions, 0 deletions
diff --git a/guides/source/active_support_core_extensions.textile b/guides/source/active_support_core_extensions.textile
index 5d0a3f82e8..181dac72cf 100644
--- a/guides/source/active_support_core_extensions.textile
+++ b/guides/source/active_support_core_extensions.textile
@@ -883,6 +883,26 @@ delegate :size, :to => :attachment, :prefix => :avatar
In the previous example the macro generates +avatar_size+ rather than +size+.
+WARNING: You can only delegate to public methods on the target object. Trying to delegate to a private or protected method will raise a +NoMethodError+ when the delegate is called.
+
+If you need to delegate to a private or protected method, you will need to implement the delegation method yourself. It's usually rather simple to do using +__send__+:
+
+<ruby>
+class Wrapper
+ def initialize
+ # Target#zizzle is a private method.
+ @target = Target.new
+ end
+
+ def zizzle(*args, &block)
+ # __send__ circumvents the private/protected mechanism.
+ @target.__send__(:zizzle, *args, &block)
+ end
+end
+</ruby>
+
+Since +__send__+ can be used to call private and protected methods, this will not raise a +NoMethodError+.
+
NOTE: Defined in +active_support/core_ext/module/delegation.rb+
h4. Redefining Methods