Class: BasicObject
- Defined in:
- lib/more/facets/basicobject.rb
Overview
BasicObject
BasicObject provides an abstract base class with no predefined methods, except for respond_to?, any method starting in _\_ (two underscore, like _\id_) as well as any method starting with <tt>instance_</ttr>.
BasicObject is useful as a base class when writing classes that depend upon method_missing (e.g. dynamic proxies).
The patterns used to reserve methods are:
/^__/, /^instance/, /^object/, /\?$/, /^\W$/,
'initialize', 'initialize_copy', 'inspect', 'dup', 'clone', 'null', 'as'
By default these are the reserved methods:
== __id__ __self__ __send__ as clone dup eql? equal? frozen?
initialize inspect instance_eval instance_of? instance_variable_get
instance_variable_set instance_variables is_a? kind_of? nil? null object_class
respond_to? tainted?
In practice only ‘as’, ‘clone’, ‘dup’ and ‘null’ have much chance of name clash. So be especially aware of these four. All the rest either begin with a ‘__’, end in a ‘?’ mark or start with the word ‘instance’ or ‘object’.
The special method #object_self allows access to the underlying object via a specialized Functor-style class access via as(Object). This binds the actual self to the subsequently called methods of Object instancea methods. So even though a method may no longer be defined for BasicObject it can still be called via this interface.
class A < BasicObject
end
a.object_self.class #=> A
a.class #=> NoMethodError
Note that #object_self used to be called __self__. Also provided is #object_class.
Direct Known Subclasses
Constant Summary collapse
- EXCLUDE =
Methods not to get rid of as they are either too important, or they are not likely to get in the way (such as methods ending in ‘?’).
In Ruby 1.9 BasicObject has only these methods:
- /^__/, “funcall”, “send”, “respond_to?”, “equal?”, “==”, “object_id”
-
NOTE The absolute bare minimum is EXCLUDE = /^(__|instance_eval$)/. But in most cases you’ll want a few extra methods like #dup too.
– TODO In the future it might be nice to allow some selectability in these via a factory method. ++
[ /^__/, /^instance_/, /^object_/, /\?$/, /^\W$/, 'initialize', 'initialize_copy', 'inspect', 'dup', 'clone', 'null', 'as' ]
Class Method Summary collapse
-
.hide(name) ⇒ Object
Undef unwanted method as long as it doesn’t match anything in the EXCLUDE list.
Instance Method Summary collapse
-
#object_self ⇒ Object
(also: #__self__)
Returns the Self functor class, which can then be used to call Kernel/Object methods on the current object.
Class Method Details
.hide(name) ⇒ Object
Undef unwanted method as long as it doesn’t match anything in the EXCLUDE list.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/more/facets/basicobject.rb', line 143 def self.hide(name) #if instance_methods.include?(name.to_s) and name !~ EXCLUDE #/^(#{EXCLUDE.join('|')})/ #if name !~ EXCLUDE and case name when *EXCLUDE # do nothing else #if ( public_instance_methods.include?(name.to_s) or # private_instance_methods.include?(name.to_s) or # protected_instance_methods.include?(name.to_s) # ) undef_method name rescue nil #end end end |