Oswald Roswell Oswald Roswell - 7 months ago 10
Ruby Question

In Ruby, what is "putting" when we command ruby to puts something?

Say we are in the Interactive Ruby Browser and run:

>> sometext = "Hello, Cruel World!"
>> puts sometext
Hello, Cruel World!
=> nil
>>


What object is doing the putting? I am sure that having a naked method sitting there seeming to "puts" itself is "syntactic sugar," but I also bet that there is some explicit name for the "object" that is performing these actions... something like Ruby::Self or Self::Thing or... what is it?

Answer

To illustrate in irb:

2.3.0 :002 > Kernel.private_instance_methods.include?(:puts)
 => true
2.3.0 :003 > self
 => main
2.3.0 :004 > self.class
 => Object
2.3.0 :005 > self.class.ancestors
 => [Object, Kernel, BasicObject]

As Kenrick Chien said in his comment below, "Kernel is a module mixed in to Object, and it provides a private method called puts. In Ruby, private methods can not be called with an explicit receiver. So when you are asking about what object is doing the putting, it's the current object in scope (self) when you call puts."