Ruby operator method calls vs. normal method calls

后端 未结 5 639
既然无缘
既然无缘 2020-12-15 23:52

I\'m wondering why calls to operator methods don\'t require a dot? Or rather, why can\'t normal methods be called without a dot?

Example

class Foo
  def +(obje         


        
5条回答
  •  孤街浪徒
    2020-12-16 00:03

    Because Ruby's syntax was designed to look roughly like popular OO languages, and those use the dot operator to call methods. The language it borrowed its object model from, Smalltalk, didn't use dots for messages, and in fact had a fairly "weird" syntax that many people found off-putting. Ruby has been called "Smalltalk with an Algol syntax," where Algol is the language that gave us the conventions you're talking about here. (Of course, there are actually more differences than just the Algol syntax.)

提交回复
热议问题