mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Document Object#public_send
This commit is contained in:
@@ -3,7 +3,7 @@ require 'active_support/core_ext/kernel/singleton_class'
|
||||
class Object
|
||||
unless Object.public_method_defined?(:public_send)
|
||||
# Backports Object#public_send from 1.9
|
||||
def public_send(method, *args, &block) # :nodoc:
|
||||
def public_send(method, *args, &block)
|
||||
# Don't create a singleton class for the object if it doesn't already have one
|
||||
# (This also protects us from classes like Fixnum and Symbol, which cannot have a
|
||||
# singleton class.)
|
||||
|
||||
@@ -452,6 +452,30 @@ Examples of +in?+:
|
||||
|
||||
NOTE: Defined in +active_support/core_ext/object/inclusion.rb+.
|
||||
|
||||
h4. +public_send+
|
||||
|
||||
This method is available by default in Ruby 1.9, and is backported to Ruby 1.8 by Active Support. Like the regular +send+ method, +public_send+ allows you to call a method when the name is not known until runtime. However, if the method is not public then a +NoMethodError+ exception will be raised.
|
||||
|
||||
<ruby>
|
||||
class Greeter
|
||||
def hello(who)
|
||||
"Hello " + who
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def secret
|
||||
"sauce"
|
||||
end
|
||||
end
|
||||
|
||||
greeter = Greeter.new
|
||||
greeter.public_send(:hello, 'Jim') # => "Hello Jim"
|
||||
greeter.public_send(:secret) # => NoMethodError
|
||||
</ruby>
|
||||
|
||||
NOTE: Defined in +active_support/core_ext/object/public_send.rb+.
|
||||
|
||||
h3. Extensions to +Module+
|
||||
|
||||
h4. +alias_method_chain+
|
||||
|
||||
Reference in New Issue
Block a user