Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

commenting restriction

In ruby 1.9, conditions on where to end a line was relaxed, so that we can now start a line with a period showing a method call. This is convenient when we have chained and non-chained methods mixed up, and want to show where the next non-chained one starts. Without this new feature, the best we could do was probably to use indentation:

method1(args1).
  method2(args2).
  method3(args3)
method4(args4).
  method5(args5).
  method6(args6)

or insert a blank line. But this was inconvenient because we have to pay attention to the indentation, and at the same time, not forget to put a period after every method call but the last one in the chain. Because of this, I created so many bugs either having an extra or missing period. With the new feature, we can do it much nicer as:

method1(args1)
.method2(args2)
.method3(args3)
method4(args4)
.method5(args5)
.method6(args6)

where the period visually functions as indented bullets.

The problem is that, when you want to insert comments before a line starting with a period, it returns an error.

method1(args1)
# method2 does blah blah
.method2(args2)
# method3 then does foo foo
.method3(args3)
method4(args4)
# method5 does blah blah
.method5(args5)
# method6 then does bar bar
.method6(args6)

# => error

or

method1(args1)
 # method2 does blah blah
.method2(args2)
 # method3 then does foo foo
.method3(args3)
method4(args4)
 # method5 does blah blah
.method5(args5)
 # method6 then does bar bar
.method6(args6)

# => error

It seems that, "#...." is not simply dropped off, but is interacting with the code in some way. What is happening? What is the exact restriction here? When the periods were at the end of a line, this did not happen.

method1(args1).
  # method2 does blah blah
  method2(args2).
  # method3 then does foo foo
  method3(args3)
method4(args4).
  # method5 does blah blah
  method5(args5).
  # method6 then does bar bar
  method6(args6)

  # => no error
like image 967
sawa Avatar asked Apr 16 '11 14:04

sawa


1 Answers

The lexical parser is probably "looser" in that it will ignore a single newline and whitespace immediately preceding a dot. It probably does not allow for multiple newlines. This splits chained statements up into an incoherent mess, and it becomes much more complicated to handle without false positives. That all said…

If you have to add inline comments, it's probably an inappropriate place for terse, chained statements.

like image 76
coreyward Avatar answered Oct 01 '22 23:10

coreyward