Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Object#singleton_method

obj.singleton_method(sym) → method

Similar to method, searches singleton method only.

Example
class Demo
  def initialize(n)
    @iv = n
  end
  def hello()
    "Hello, @iv = #{@iv}"
  end
end

k = Demo.new(99)
def k.hi
  "Hi, @iv = #{@iv}"
end
m = k.singleton_method(:hi)
m.call   #=> "Hi, @iv = 99"
m = k.singleton_method(:hello) #=> NameError
See also #singleton_class #singleton_methods #send #taint also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0