CoffeeScript:意外的INDENT错误
我正在Rails 3.1应用程序中试用CoffeeScript。但是,我无法让
弄清楚如何在CoffeeScript中打破长行,而不会出现
以上的错误
I am trying out CoffeeScript in my Rails 3.1 app. However, I am not able to figure out how to break long lines in CoffeeScript without getting the above error
例如,如何/何地您会打破下面的代码行吗?
For example, how/where would you break the following line of code
alert x for x in [1,2,3,4,5] when x > 2
如果您想要类似的东西
alert x for
x in [1,2,3,4,5]
when x > 2
在我的vimrc中,我设置了
In my vimrc, I have set
ts=2, sw=2 and I expand tabs.
但是,我无法获得像上面这样简单的代码才能正常工作。
And yet, I cannot get something as simple as the line above to work properly.
我的Gemfile.lock显示了带有coffee-script-source 1.1.3的coffee-script-2.2.0
My Gemfile.lock shows coffee-script-2.2.0 with coffee-script-source 1.1.3
如果您的理解力太长,可以使用@brandizzi提到的 \
来打破它,但是我认为您可能会只需在有意义的地方使用理解并在不知道的地方扩展到常规代码,便会更好:
If you have a comprehension that is too long you can break it with \
as @brandizzi mentions, but I think you might have better luck just using comprehensions where they make sense and expanding to 'regular' code where they don't:
alert x for x in [1,2,3,4,5] when x > 2
...可以重写为...
...can be rewritten as...
for x in [1,2,3,4,5]
alert x if x > 2
...甚至...
for x in [1,2,3,4,5]
if x > 2
alert x
换句话说,理解力是简短,简洁的摘要的语法糖-您不必将它们用于所有操作。
In other words, comprehensions are syntactic sugar for short, concise snippets - you don't have to use them for everything.