saw*_*awa 6 ruby comments newline indentation ruby-1.9
在ruby 1.9中,放宽了一条线的结束条件,以便我们现在可以开始一个显示方法调用的句点的行.当我们将链式和非链式方法混合在一起时,这很方便,并且想要显示下一个非链式方法的开始位置.如果没有这个新功能,我们可以做的最好的就是使用缩进:
method1(args1).
method2(args2).
method3(args3)
method4(args4).
method5(args5).
method6(args6)
Run Code Online (Sandbox Code Playgroud)
或插入一个空行.但这很不方便,因为我们必须注意缩进,同时,不要忘记在每个方法调用之后设置一个句点,但链中的最后一个.因此,我创建了许多错误,要么有额外的或缺少一段时间.有了这个新功能,我们可以做得更好:
method1(args1)
.method2(args2)
.method3(args3)
method4(args4)
.method5(args5)
.method6(args6)
Run Code Online (Sandbox Code Playgroud)
这个时期在视觉上起着缩进的子弹的作用.
问题是,当您想要在以句点开头的行之前插入注释时,它会返回错误.
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
Run Code Online (Sandbox Code Playgroud)
要么
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
Run Code Online (Sandbox Code Playgroud)
似乎"#...."不是简单地放弃,而是以某种方式与代码交互.怎么了?这里的确切限制是什么?如果期间在一条线的末尾,则不会发生这种情况.
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
Run Code Online (Sandbox Code Playgroud)
词法解析器可能比较“宽松”,因为它会忽略紧接在点之前的单个换行符和空格。它可能不允许多个换行符。这将连锁语句分裂成不连贯的混乱,并且在没有误报的情况下处理起来变得更加复杂。都说了\xe2\x80\xa6
\n\n如果您必须添加内联注释,那么它可能不适合放置简洁的链接语句。
\n