Wednesday, February 20, 2013

Ruby chaining and comments

I have run across interesting feature in ruby when I tried to write chain methods separated in new lines. Ruby 1.9+ enables dots in new line at the beginning of command, not only at the end of last line, like this:

"somestring"
  .upcase
  .first

But, if you want to put comment in separate line before chain method call then ruby throws syntax error. So, this is invalid expression in ruby:

"somestring"
  .upcase
  #take first letter
  .first

If you want this to be valid you have to put dots at the end of the line.

"somestring".
  upcase.
  # take first letter
  first

Tuesday, February 12, 2013

MS Excel doesn't read UTF-8 CSV file correctly

I had an issue where MS Excel would not open CSV file generated in node.js in correct encoding. Instead of opening it with UTF-8 encoding it would use default ANSI encoding. To resolve this I had to add BOM at the beginning of a file. So my write file function would look like:


fs.writeFile(filename, '\ufeff' + data, 'utf8', function(err) {
    if (err) console.error(err);
});

This still doesn't resolved the issue MS Excel has, where it doesn't recognize C in CSV.

Reference:

  1. StackOverflow