#--
# BasicObject
#
# Copyright (c) 2005 Thomas Sawyer
# based on BlankSlate, Copyright (c) 2004 by Jim Weirich
#
# Ruby License
#
# This module is free software. You may use, modify, and/or redistribute this
# software under the same terms as Ruby.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
#
# ==========================================================================
# Revision History
# ==========================================================================
# 2005.10.26
# * preserved a few addtional methods --everyhting ending in ?, dup and
# object_id.
# * changed name from BlankSlate to BasicObject
#
# 2005.04.28 trans
# * ported to mega and added __class__
# ==========================================================================
#++
require 'nano/kernel/__class__'
# :title: 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 instance_.
#
# BasicObject is useful as a base class when writing classes that
# depend upon method_missing (e.g. dynamic proxies).
#
# The pattern used to reserve methods is:
#
# /(^__|^instance_|^object_|^null$|^inspect$|^dup$|\?$)/
#
# By default these are the reserved methods:
#
# __id__ __class__ __send__ dup eql? equal? frozen? inspect instance_eval
# instance_of? instance_variable_get instance_variable_set instance_variables
# is_a? kind_of? nil? null object_id respond_to? tainted?
#
class BasicObject
EXCLUDE = /(^__|^instance_|^object_|^null$|^inspect$|^dup$|\?$)/
class << self
def hide(name)
#if instance_methods.include?(name.to_s) and name !~ EXCLUDE #/^(#{EXCLUDE.join('|')})/
if method_defined?(name) and name !~ EXCLUDE
undef_method name
end
end
end
instance_methods.each { |m| hide(m) }
end
# This must be down here to prevent circular dependency
require 'nano/kernel/__meta__'
# 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 a hook
# in the Object and Kernel classes that will hide any defined.
module Kernel
class << self
#alias_method :blank_slate_method_added, :method_added
#def method_added(name)
# return if name == :blank_slate_method_added
# blank_slate_method_added(name)
madded = instance_method(:method_added)
define_method(:method_added) do |name|
madded.bind(self).call(name)
return if self != Kernel
BasicObject.hide(name)
end
end
end
class Object
class << self
madded = instance_method(:method_added)
define_method(:method_added) do |name|
madded.bind(self).call(name)
return if self != Object
BasicObject.hide(name)
end
end
end
# _____ _
# |_ _|__ ___| |_
# | |/ _ \/ __| __|
# | | __/\__ \ |_
# |_|\___||___/\__|
#
# TODO
=begin #testing
=end