CoffeeScript: unexpected error INDENT

I am trying to use CoffeeScript in a Rails 3.1 application. However, I cannot figure out how to break long lines in CoffeeScript without getting the above error

For example, how / where do you break the following line of code

alert x for x in [1,2,3,4,5]  when x > 2

if you want something like

alert x for
  x in [1,2,3,4,5]
  when x > 2

In my vimrc I installed

 ts=2, sw=2 and I expand tabs. 

And yet, I cannot get something as simple as the line above to work correctly.

My Gemfile.lock shows coffee script -2.2.0 with coffee - script - source 1.1.3

0
source share
3 answers

, , \, @brandizzi, , , , "" , ":

alert x for x in [1,2,3,4,5]  when x > 2

... ...

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

, , - .

+6

, . , . , , :

for x in [1..5] when x > 2
  alert x

, , CoffeeScript http://jashkenas.github.com/coffee-script/, , , .

+4

I don’t understand the internal details of CoffeeScript syntax, so I can’t say what happens in less detail. However, the error is somewhat clear: you cannot put a new line between forand its iterator variable. Also, you have not received this error yet, but you cannot put a new line between the iterated object and the sentence when. However, if you really want to do this, it's easy: place a backslash at the end of the first and second lines.

console.log x for \
    x in [1,2,3,4,5] \
    when x > 2
+1
source

All Articles