Question

I'm attempting to write a simple github API call in coffeescript, but have been running into inconsistencies which I can't explain between coffeescript.org and the coffee-rails gem. I have checked for a mix of tabs/spaces more times than I can count, and feel there must be something subtle or stupid that I'm missing.

Original Code

$ ->
  gistids = ['5100088']
  for gistid in gistids
    $.ajax
      url: 'https://api.github.com/gists/'+gistid,
      type: 'GET',
      dataType: 'jsonp'
    .success (gistdata) ->
      console.log(gistdata.data.files)
    .fail (e) ->
      console.log(e)

Coffeescript.org Compilation

It happily compiles to the following on coffeescript.org (and when run via the console, functions exactly as expected).

$(function() {
  var gistid, gistids, _i, _len, _results;
  gistids = ['5100088'];
  _results = [];
  for (_i = 0, _len = gistids.length; _i < _len; _i++) {
    gistid = gistids[_i];
    _results.push($.ajax({
      url: 'https://api.github.com/gists/' + gistid,
      type: 'GET',
      dataType: 'jsonp'
    }).success(function(gistdata) {
      return console.log(gistdata.data.files);
    }).fail(function(e) {
      return console.log(e);
    }));
  }
  return _results;
});

Rails-coffee gem Compilation (this is my output)

The alternate compilation is obviously destined for failure, and doesn't seem even close to what I'm trying to achieve. It almost looks like the for loop is trying to execute the contents as you'd expect in eat food for food in ['toast', 'cheese', 'wine'] type syntax?

This code is also output if you use another online 'try coffeescript' type site, such as http://www.compileonline.com/try_coffeescript_online.php

  $(function() {
    var gistid, gistids, _i, _len, _ref, _results;
    _ref = gistids($.ajax({
      url: 'https://api.github.com/gists/' + gistid,
      type: 'GET',
      dataType: 'jsonp'.success(function(gistdata) {
        return console.log(gistdata.data.files).fail(function(e) {
          return console.log(e);
        });
      })
    }));
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      gistid = _ref[_i];
      _results.push(gistids = ['5100088']);
    }
    return _results;
  });

What have I missed? Is there a major change between the versions being used, a compilation flag difference, a small syntactical error which is being overlooked, or have I gotten it completely wrong?

Était-ce utile?

La solution

This is no longer an issue, but am no closer to finding an answer as to why it has changed.

I was able to change and recompile the code, with changes being reflected in the code (still incorrectly compiled). I decided to retype the code by hand in another coffeescript file, which worked perfectly. I then copy/pasted over my retyped code, and it still worked in the other file.

I then swapped the contents from my existing coffeescript file over to the file I was having issues with, and it wouldn't even compile. I renamed the file, thinking that perhaps there was some naming restrictions I was unaware of, and it began to compile correctly. I then changed the file name back, and it still compiles perfectly. Note that above I mentioned the compilation was being updated with changes, just compiling incorrectly (it wasn't an old version).

The strange thing here, is that I was able to undo all changes and recover the code which was having issues. Copying this into compileronline still caused the same problems, but copying the new code worked perfectly. I did a diff comparison between the old and new code, and it reported no differences, even though the above was entirely repeatable.

All I can assume is this was a whitespace/character issue which wasn't being shown in the diff, even though retyping by hand didn't seem to fix it the first time, until the file was changed. I have yelled at the world about the injustice of this enough and I need to move on, but I would be very interested if anyone had a more solid reason for this.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top