If `self` is always the implied receiver in Ruby, why doesn't `self.puts` work?

后端 未结 4 1733
野性不改
野性不改 2021-01-12 19:22

In Ruby, my understanding is that self is the implied receiver for any bare method call. However:

~: irb
>> puts \"foo\"
foo
=> nil
>         


        
4条回答
  •  粉色の甜心
    2021-01-12 19:49

    You're correct that self is the implied receiver when you don't specify one explicitly. The reason that you're not allowed to do self.puts is that you may not call private methods with an explicit receiver (even if that receiver is self) and as the error message says, puts is a private method.

提交回复
热议问题