Class | BasicObject
In: | lib/facets/more/basicobject.rb
Parent: | Object

# 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.

### Methods

__self__ hide object_self

### Constants

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.

### Public Class methods

hide(name)

Undef unwanted method as long as it doesn‘t match anything in the EXCLUDE list.

### Public Instance methods

__self__()

Alias for object_self

object_self()

Returns the Self functor class, which can then be used to call Kernel/Object methods on the current object.

[Validate]