What is the object in Ruby "hello world"?

If everything is an object in Ruby, 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?

+8
object methods oop ruby puts
source share
3 answers

fetters

To find a method, you can call:

 method(:puts) #=> #<Method: Object(Kernel)#puts> 

So puts is a Kernel defined method available for every object.

Kernel # put

 puts "Hello world" 

in fact

 self.puts( String.new("Hello world") ) 

Where self is the main object.

So puts "hello world" is:

Notes

Please note that if you follow

 self.puts( String.new("Hello world") ) 

You will receive an error message:

 private method `puts' called for main:Object (NoMethodError) 

Because every Kernel method becomes available for every object, but as a private method. You will need:

 self.send(:puts, String.new("Hello world") ) 

Test

Another way to check:

 module Kernel def my_puts(*args) print "Calling Kernel#my_puts on #{self} with #{args}\n" print "Now delegating to Kernel#puts on #{self} with #{args} :\n" puts(*args) end end my_puts "Hello world" 

It outputs:

 Calling Kernel#my_puts on main with ["Hello world"] Now delegating to Kernel#puts on main with ["Hello world"] : Hello world 

Cm? All this is an object, although it may not look like.

2 + 3

In the same vein: 2+3 is actually Integer(2).+( Integer(3) ) .

+10
source share

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 #=> 'main' inspect #=> 'main' 

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.

+10
source share

"Hello world" is a string object defined as a literal constant, given as an argument to the puts method

puts "Hello world" is actually an easy way to do

 puts myFoo 

where myFoo must be a valid object ... in your case myFoo = "Hello world"

+1
source share

All Articles