T
Trans
Hi--
I've been hadling a lot of OpenStruct like classes the last few days.
It isn;t alwasy smooth sailing. I've been thinking about accessing
certain "metainfo" about an object from the outside rather then from
the inside to ensure certain results. A friend say it goes against the
grain of OOP and polymorphism, but I wonder if certian "metainfo"
shouldn't? What do others think?
Also have a look at my example code below.
Thanks,
T.
---
# The Inspect module provides a guaranteed means of inspecting
# an object and seeing it for what it really is. Using a
# separate module prevents the methods from being overridden
# in the class/object being inspected. For example using the
# Inspect module methods, a Proxy object will be seen as a
# Proxy object and not it's delegate.
module Inspect
extend self
OBJECT_INSPECTION_METHODS = [
:send, :dup, :clone, bject_id,
:class, :is_a?, :kind_of?,
:respond_to?, :method, :methods,
:instance_variable_get, :instance_variable_set,
:instance_eval
]
CLASS_INSPECTION_METHODS = [
:instance_methods, :class_eval, :module_eval, :method_defined?
]
def define_inspection_method( name, old_name=name, from=Object )
if from.method_defined?( old_name )
im = from.instance_method( old_name )
define_method(name) do |slf,*args|
im.bind(slf).call(*args)
end
end
end
define_inspection_method( :id, bject_id )
define_inspection_method( :var_get, :instance_variable_get )
define_inspection_method( :var_set, :instance_variable_set )
OBJECT_INSPECTION_METHODS.each do |m|
define_inspection_method( m )
end
define_inspection_method( :class_of?, :===, Class )
CLASS_INSPECTION_METHODS.each do |m|
define_inspection_method( m, m, Class )
end
def nil?(obj) ; is_a?(obj, NilClass) ; end
def false?(obj) ; is_a?(obj, FalseClass) ; end
def true?(obj) ; is_a?(obj, TrueClass) ; end
def bool?(obj) ; is_a?(obj, FalseClass) or is_a?(obj, TrueClass) ;
end
def null?( obj )
defined?(NullClass) and is_a?(slf,NullClass)
end
def nack?( obj )
defined?(NackClass) and is_a?(slf,NackClass)
end
def f?( obj )
return true if is_a?(obj,NilClass)
return true if defined?(NullClass) and is_a?(obj,NullClass)
return true if defined?(NackClass) and is_a?(obj,NackClass)
false
end
def t?( obj )
return false if is_a?(obj,NilClass)
return false if defined?(NullClass) and is_a?(obj,NullClass)
return false if defined?(NackClass) and is_a?(obj,NackClass)
true
end
end
class Object
def instance
@_instance ||= Functor.new { |op, *args| Inspect.__send__(op, self,
*args) }
end
end
I've been hadling a lot of OpenStruct like classes the last few days.
It isn;t alwasy smooth sailing. I've been thinking about accessing
certain "metainfo" about an object from the outside rather then from
the inside to ensure certain results. A friend say it goes against the
grain of OOP and polymorphism, but I wonder if certian "metainfo"
shouldn't? What do others think?
Also have a look at my example code below.
Thanks,
T.
---
# The Inspect module provides a guaranteed means of inspecting
# an object and seeing it for what it really is. Using a
# separate module prevents the methods from being overridden
# in the class/object being inspected. For example using the
# Inspect module methods, a Proxy object will be seen as a
# Proxy object and not it's delegate.
module Inspect
extend self
OBJECT_INSPECTION_METHODS = [
:send, :dup, :clone, bject_id,
:class, :is_a?, :kind_of?,
:respond_to?, :method, :methods,
:instance_variable_get, :instance_variable_set,
:instance_eval
]
CLASS_INSPECTION_METHODS = [
:instance_methods, :class_eval, :module_eval, :method_defined?
]
def define_inspection_method( name, old_name=name, from=Object )
if from.method_defined?( old_name )
im = from.instance_method( old_name )
define_method(name) do |slf,*args|
im.bind(slf).call(*args)
end
end
end
define_inspection_method( :id, bject_id )
define_inspection_method( :var_get, :instance_variable_get )
define_inspection_method( :var_set, :instance_variable_set )
OBJECT_INSPECTION_METHODS.each do |m|
define_inspection_method( m )
end
define_inspection_method( :class_of?, :===, Class )
CLASS_INSPECTION_METHODS.each do |m|
define_inspection_method( m, m, Class )
end
def nil?(obj) ; is_a?(obj, NilClass) ; end
def false?(obj) ; is_a?(obj, FalseClass) ; end
def true?(obj) ; is_a?(obj, TrueClass) ; end
def bool?(obj) ; is_a?(obj, FalseClass) or is_a?(obj, TrueClass) ;
end
def null?( obj )
defined?(NullClass) and is_a?(slf,NullClass)
end
def nack?( obj )
defined?(NackClass) and is_a?(slf,NackClass)
end
def f?( obj )
return true if is_a?(obj,NilClass)
return true if defined?(NullClass) and is_a?(obj,NullClass)
return true if defined?(NackClass) and is_a?(obj,NackClass)
false
end
def t?( obj )
return false if is_a?(obj,NilClass)
return false if defined?(NullClass) and is_a?(obj,NullClass)
return false if defined?(NackClass) and is_a?(obj,NackClass)
true
end
end
class Object
def instance
@_instance ||= Functor.new { |op, *args| Inspect.__send__(op, self,
*args) }
end
end