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.
Since Ruby is very dynamic, methods added to the ancestors of BasicObject after BasicObject is defined will show up in the list of available BasicObject methods. We handle this by defining hooks in Object, Kernel and Module that will hide any defined.
EXCLUDE | = | [ /^__/, /^instance_/, /^object_/, /\?$/, /^\W$/, 'initialize', 'initialize_copy', 'inspect', 'dup', 'clone', 'null', 'as' |
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. |
Undef unwanted method as long as it doesn‘t match anything in the EXCLUDE list.
[ show source ]
# File lib/lore/facets/basicobject.rb, line 177 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
Alias for object_self
Returns the Self functor class, which can then be used to call Kernel/Object methods on the current object.
[ show source ]
# File lib/lore/facets/basicobject.rb, line 122 def object_self @__object_self__ ||= As.new(self, ::Object) end