If all objects in Ruby,
This is a bit misleading. For example. syntax is not an object, blocks are not objects, conditional expression is not an object.
A better expression would be: "every value is an object."
to such an extent that even mathematical operators are methods applied to objects when I write:
puts "Hello world"
The puts method and the parameter are "Hello world", but what is this object?
There are two objects. I suspect that you really wanted to ask: "What is the recipient object." (Actually, there are many more objects, but now focus on the receiver and the argument.)
Sending a message always has a recipient object, that is, the object to which you send the message. If the recipient object is not explicitly written out, it is implicitly considered self (a pseudo-variable that always evaluates the "current object"). Now the question is: what (or "who") is self here?
This, by the way, is one of the most important questions you should ask yourself when writing Ruby code. Sent messages from the sender are sent to self , instance variables are scanned to self . You should always know which self object is at the point where you are writing your code.
In this particular case, when you write code on which rubists call the top level, self is a predefined Rubyists call main object. It actually has no name, and there is no default link for it, but 'main' is what it returns when you inspect it or convert it to a string using to_s :
to_s
The second object that you have already correctly defined is the String object obtained by evaluating the string literal "Hello World" .
Here, however, more facilities are involved. For example, main is an instance of Object , so Object must also exist. "Hello World" is an instance of String , a String must exist. String and Object are both classes, that is, objects that are instances of the Class class, so the Class must exist. Object superclass - BasicObject . Class superclass Module . Object is mixed in Kernel (which is an instance of the Module class). Kernel#puts returns nil , which is an instance of NilClass . Kernel#puts is just a helper method that delegates to IO#puts by calling $stdout.puts .
So, in addition to main and "Hello World" , at least the following objects are involved: nil , NilClass , String , Class , Module , Object , Kernel , BasicObject , IO and the IO object assigned to the global variable $stdout , which represents the standard output stream your Ruby process.
These are 12 objects that are directly and closely related to the execution of the code fragment.
In fact, there are many more objects in a simple Ruby program:
ruby --disable-gems --disable-did_you_mean -e 'p ObjectSpace.count_objects[:TOTAL]'
On my system, this number displays about 9780 objects. Here are some of them: Hash created by ObjectSpace::count_objects , and the keys and values inside the Hash (for example, Symbol object :TOTAL ), and, of course, String , created by trying to print the result. But this is of the order of ~ 35 objects, so almost 10,000 objects are still involved in the execution, well, basically, an empty program that does nothing.