aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/object/public_send.rb
blob: 2e77a22c4ba00286e66a6e2ce73f19882cb1b8ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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)
      # 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.)
      klass = singleton_methods.any? ? self.singleton_class : self.class

      if klass.public_method_defined?(method)
        send(method, *args, &block)
      else
        if klass.private_method_defined?(method)
          raise NoMethodError, "private method `#{method}' called for #{inspect}"
        elsif klass.protected_method_defined?(method)
          raise NoMethodError, "protected method `#{method}' called for #{inspect}"
        else
          raise NoMethodError, "undefined method `#{method}' for #{inspect}"
        end
      end
    end
  end
end