0

Why are parentheses required in this?

I am looking for a purely syntax related reasoning. This is not a question about what should or not be in ruby for beauty or whatever.

not(['a','u','i','o','e'].include? s[0].downcase)
#this third line will error!
not ['a','u','i','o','e'].include? s[0].downcase

#you can also incorrectly represent this as
not['a','u','i','o','e'].include?(s[0].downcase)
#but correctly as this with the higher precedence !
!['a','u','i','o','e'].include?(s[0].downcase)
#but then again incorrectly as
!['a','u','i','o','e'].include? s[0].downcase

In general I think the whitespaces with (), {}, ;, \n, and end are oddly difficult to get correct in ruby. It has something to do with precendence. Can someone show me a mapping of where and not these invisible () are when executing the above code.

jStaff
  • 650
  • 1
  • 9
  • 25
  • What makes you think parenthese are required in this case? – Nils Landt Feb 01 '17 at 18:12
  • The interpreter errors out if I don't have parentheses in the ones marked as incorrect. – jStaff Feb 01 '17 at 18:17
  • On what Ruby version is that? – Nils Landt Feb 01 '17 at 18:34
  • ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux] – jStaff Feb 01 '17 at 18:51
  • While parenthesis are optional with methods, their use allow you to tell the interpreter exactly what you want it to do and when. I recommend their use until you are very familiar with Ruby's parsing. – the Tin Man Feb 01 '17 at 20:09
  • 1
    Can't reproduce. Only the line starting with `not[` gives me a syntax error. That's not an operator precidence problem, it simply appears that `not[` cannot be parsed by Ruby. `not(` works while `not{` fails as well. – user229044 Feb 01 '17 at 20:25

0 Answers0