[DOC] Mention the omission of a superclass when reopening a class

This commit is contained in:
Kouhei Yanagita 2023-10-22 20:12:16 +09:00 committed by GitHub
parent e721a7aec7
commit b84e6fe93e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,9 +40,9 @@ functionality:
remove_method :my_method remove_method :my_method
end end
Reopening classes is a very powerful feature of Ruby, but it is best to only Reopening modules (or classes) is a very powerful feature of Ruby, but it is
reopen classes you own. Reopening classes you do not own may lead to naming best to only reopen modules you own. Reopening modules you do not own may lead
conflicts or difficult to diagnose bugs. to naming conflicts or difficult to diagnose bugs.
== Nesting == Nesting
@ -259,6 +259,28 @@ includes a minimum of built-in methods. You can use BasicObject to create an
independent inheritance structure. See the BasicObject documentation for independent inheritance structure. See the BasicObject documentation for
further details. further details.
Just like modules, classes can also be reopened. You can omit its superclass
when you reopen a class. Specifying a different superclass than the previous
definition will raise an error.
class C
end
class D < C
end
# OK
class D < C
end
# OK
class D
end
# TypeError: superclass mismatch for class D
class D < String
end
== Inheritance == Inheritance
Any method defined on a class is callable from its subclass: Any method defined on a class is callable from its subclass: