Module: ObjectSpace
- Defined in:
- lib/core/facets/objectspace/classes.rb,
lib/core/facets/objectspace/reflect.rb
Defined Under Namespace
Classes: Reflector
Class Method Summary collapse
-
.classes ⇒ Array[Class]
All the classes in the object space.
-
.reflect(obj) ⇒ Object
Reflection ensures that information about an object is actual according to Ruby’s Kernel definitions, just in case such methods have been overridden.
Class Method Details
.classes ⇒ Array[Class]
Returns All the classes in the object space.
4 5 6 7 8 |
# File 'lib/core/facets/objectspace/classes.rb', line 4 def self.classes klasses = [] each_object(Class){|o| klasses << o} klasses end |
.reflect(obj) ⇒ Object
Reflection ensures that information about an object is actual according to Ruby’s Kernel definitions, just in case such methods have been overridden.
ObjectSpace.reflect("object").object_id
There is also a global short-cut for this method to ease meta-programming with it.
$ref["object"].class
Typically theis method will be used to gather the object’s id, as in the example given, or it’s class, but any Kernel method can be used.
Care should be taken in utilizing this technique. In most cases it is not needed, but in certain cases is useful for improving the robustness of meta-programming solutions.
Note that this is also equivalent to using as(Kernel) …
"object".as(Kernel).object_id
But obviously, in this case there is the risk that #as has be overridden too.
31 32 33 34 35 36 37 |
# File 'lib/core/facets/objectspace/reflect.rb', line 31 def self.reflect(obj) ## TODO: use this after 1.8.6 support discontinued ## Functor.new do |op, *a, &b| ## Kernel.instance_method(op).bind(obj).call(*a, &b) ## end Reflector.new(obj) end |