|
1 | 1 | # <!-- rdoc-file=object.c --> |
2 | | -# BasicObject is the parent class of all classes in Ruby. It's an explicit |
3 | | -# blank class. |
| 2 | +# `BasicObject` is the parent class of all classes in Ruby. In particular, |
| 3 | +# `BasicObject` is the parent class of class Object, which is itself the default |
| 4 | +# parent class of every Ruby class: |
4 | 5 | # |
5 | | -# BasicObject can be used for creating object hierarchies independent of Ruby's |
6 | | -# object hierarchy, proxy objects like the Delegator class, or other uses where |
7 | | -# namespace pollution from Ruby's methods and classes must be avoided. |
| 6 | +# class Foo; end |
| 7 | +# Foo.superclass # => Object |
| 8 | +# Object.superclass # => BasicObject |
8 | 9 | # |
9 | | -# To avoid polluting BasicObject for other users an appropriately named subclass |
10 | | -# of BasicObject should be created instead of directly modifying BasicObject: |
| 10 | +# `BasicObject` is the only class that has no parent: |
11 | 11 | # |
12 | | -# class MyObjectSystem < BasicObject |
13 | | -# end |
| 12 | +# BasicObject.superclass # => nil |
14 | 13 | # |
15 | | -# BasicObject does not include Kernel (for methods like `puts`) and BasicObject |
16 | | -# is outside of the namespace of the standard library so common classes will not |
17 | | -# be found without using a full class path. |
| 14 | +# Class `BasicObject` can be used to create an object hierarchy (e.g., class |
| 15 | +# Delegator) that is independent of Ruby's object hierarchy. Such objects: |
18 | 16 | # |
19 | | -# A variety of strategies can be used to provide useful portions of the standard |
20 | | -# library to subclasses of BasicObject. A subclass could `include Kernel` to |
21 | | -# obtain `puts`, `exit`, etc. A custom Kernel-like module could be created and |
22 | | -# included or delegation can be used via #method_missing: |
| 17 | +# * Do not have namespace "pollution" from the many methods provided in class |
| 18 | +# Object and its included module Kernel. |
| 19 | +# * Do not have definitions of common classes, and so references to such |
| 20 | +# common classes must be fully qualified (`::String`, not `String`). |
23 | 21 | # |
24 | | -# class MyObjectSystem < BasicObject |
25 | | -# DELEGATE = [:puts, :p] |
| 22 | +# A variety of strategies can be used to provide useful portions of the Standard |
| 23 | +# Library in subclasses of `BasicObject`: |
26 | 24 | # |
27 | | -# def method_missing(name, *args, &block) |
28 | | -# return super unless DELEGATE.include? name |
29 | | -# ::Kernel.send(name, *args, &block) |
30 | | -# end |
| 25 | +# * The immediate subclass could `include Kernel`, which would define methods |
| 26 | +# such as `puts`, `exit`, etc. |
| 27 | +# * A custom Kernel-like module could be created and included. |
| 28 | +# * Delegation can be used via #method_missing: |
31 | 29 | # |
32 | | -# def respond_to_missing?(name, include_private = false) |
33 | | -# DELEGATE.include?(name) or super |
34 | | -# end |
35 | | -# end |
| 30 | +# class MyObjectSystem < BasicObject |
| 31 | +# DELEGATE = [:puts, :p] |
36 | 32 | # |
37 | | -# Access to classes and modules from the Ruby standard library can be obtained |
38 | | -# in a BasicObject subclass by referencing the desired constant from the root |
39 | | -# like `::File` or `::Enumerator`. Like #method_missing, #const_missing can be |
40 | | -# used to delegate constant lookup to `Object`: |
| 33 | +# def method_missing(name, *args, &block) |
| 34 | +# return super unless DELEGATE.include? name |
| 35 | +# ::Kernel.send(name, *args, &block) |
| 36 | +# end |
41 | 37 | # |
42 | | -# class MyObjectSystem < BasicObject |
43 | | -# def self.const_missing(name) |
44 | | -# ::Object.const_get(name) |
45 | | -# end |
46 | | -# end |
| 38 | +# def respond_to_missing?(name, include_private = false) |
| 39 | +# DELEGATE.include?(name) |
| 40 | +# end |
| 41 | +# end |
47 | 42 | # |
48 | 43 | # ### What's Here |
49 | 44 | # |
|
60 | 55 | # `self`. |
61 | 56 | # * #instance_exec: Executes the given block in the context of `self`, passing |
62 | 57 | # the given arguments. |
| 58 | +# * #method_missing: Called when `self` is called with a method it does not |
| 59 | +# define. |
| 60 | +# * #singleton_method_added: Called when a singleton method is added to |
| 61 | +# `self`. |
| 62 | +# * #singleton_method_removed: Called when a singleton method is removed from |
| 63 | +# `self`. |
| 64 | +# * #singleton_method_undefined: Called when a singleton method is undefined |
| 65 | +# in `self`. |
63 | 66 | # |
64 | 67 | class BasicObject |
65 | 68 | # <!-- |
|
0 commit comments