Ruby Inspect, a comprehensive review of the programming language Ruby
Ruby has both computational reflection and structural reflection in Ruby.
We can see the state of living objects in a Ruby process using ObjectSpace.each_object() method [1]. For example, we can print all the living Robot objects in the current Ruby process using the codes below.
computational_reflection.rb
class Robot
def initialize(name)
@name = name
end
def inspect
"Hello I am robot #{@name}" # Return a human-readable string when printing the object
end
end
robot_1 = Robot.new("Tom")
robot_2 = Robot.new("Jerry")
ObjectSpace.each_object(Robot) {|x| p x} # This print the living Robot objects
Source code: https://github.com/yc015/Ruby-Inspect/blob/main/src/computational_reflection.rb
The codes above will print the living Robot objects (robot_1 and robot_2).
The program will print:
Hello I am robot Tom
Hello I am robot Jerry
The ObjectSpace.each_object shows that Ruby has computational reflection, and we can check the current state of the system.
We can modify the state of the system in Ruby. For example, we can define and add a method to an instance after it was created using define_singleton_method [2].
structural_reflection.rb
str_hello = "Hello world!" # str_hello is a String object
str_hello.define_singleton_method(:my_class_name) do
"My class name is String"
end
str_hello.my_class_name # This line will return "My class name is String"
Source code: https://github.com/yc015/Ruby-Inspect/blob/main/src/structural_reflection.rb
The codes above will print:
My class name is String
The codes above add a new method, my_class_name, to the existing String object str_hello.
References:
[1] http://phrogz.net/programmingruby/ospace.html Reflection, ObjectSpace, and Distributed Ruby - Programming Ruby
[2] https://apidock.com/ruby/Object/define_singleton_method Ruby’s Documentation for define_singleton_method - APIDock