Reopening monkey patching Singleton Classes

suggest change

There are three ways to reopen a Singleton Class

class Example
end

Example.singleton_class.class_eval do
  def foo
    :foo
  end
end

Example.foo #=> :foo

—``` class Example end

class << Example def bar :bar end end

Example.bar #=> :bar

---```
class Example
end

def Example.baz
  :baz
end

Example.baz #=> :baz

Every object has a singleton class which you can access

class Example
end
ex1 = Example.new
def ex1.foobar
  :foobar
end
ex1.foobar #=> :foobar

ex2 = Example.new
ex2.foobar #=> NoMethodError

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents