1

I thought it was kinda cool that super and super() offer different behaviours, with the latter calling the parent method without passing it arguments.

This is the first case I'm aware of of a ruby method/keyword that behaves differently depending on whether the () are included (more on that here).

Are there any other methods/keywords in the ruby programming language which exhibit different behaviour depending on whether the () are included?

stevec
  • 41,291
  • 27
  • 223
  • 311
  • 1
    You can have a similar effect when having a local variable `foo` and a method `foo` in the same scope: `foo` refers to the variable and `foo()` invokes the method. – Stefan Jan 10 '22 at 10:34

2 Answers2

2

It is impossible for a Ruby method to know what syntax was used for the message send, therefore, a method cannot possibly behave different depending on whether or not the message send includes ().

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • Thank you. In light of `super` being a keyword (rather than method as I first assumed), I've altered the question to also include keywords. +1 – stevec Jan 08 '22 at 23:56
  • 1
    While I agree that the caller cannot know how the call was made: Doesn't @stevec's example with `super` vs `super()` contradict what you are saying? There is different behavior based on parentheses in this case. Stevec is asking if (voluntary) parentheses matter in other cases. – Christopher Oezbek Jan 09 '22 at 07:03
  • @ChristopherOezbek: `super` is a keyword, not a method. – Jörg W Mittag Jan 09 '22 at 09:33
  • 1
    @JörgWMittag Yes, but does it matter for the question? – Christopher Oezbek Jan 09 '22 at 09:54
  • @ChristopherOezbek: Please, check the timestamps. At the time I wrote I my answer, the question explicitly only asked about methods. It was heavily modified after I wrote my answer (which is generally considered impolite on SO). – Jörg W Mittag Jan 09 '22 at 10:26
  • Ah, I didn't see that – Christopher Oezbek Jan 09 '22 at 12:39
1

Methods in Ruby behave same whether you use parenthesis or not. According to the Ruby style guide, you can omit parenthesis after a method call when you don't pass any arguments. It's true that some Ruby programmers liberally omit parenthesis but this will not make their methods behave differently.

eva
  • 43
  • 5
  • Thank you. Please note that I updated the question to include keywords (as I hadn’t realised `super` was a keyword, not a method). So this is a good answer to part of the question (about methods), are you also able to comment on whether any other ruby keywords allow or are affected by the use of parentheses? – stevec Jan 09 '22 at 09:13
  • How does this answer the question ? More or less every ruby developer knows this fact about parenthesis. Read the question carefully before you answer it. – Rajagopalan Jan 09 '22 at 09:33
  • Rajagopalan, the question was only about parenthesis in methods, the author has updated it since as he points in his comment. If you knew the correct answer to the updated question, you could have provided it. – eva Apr 18 '22 at 06:42