Skip to content

Encoding problem in coffee-script #559

whitetrefoil opened this Issue Aug 15, 2012 · 2 comments

2 participants


OS: Windows XP in English
Ruby: 1.9.3-p194
Sinatra: 1.3.2

All files are saved in utf-8 without BOM


"something 中文 something"
# asdf.rb

# ...
  get "/js/:name.js" do
    coffee :"#{params[:name]}", :views => "public/js/coffee"

Then open http://localhost:4567/js/asdf.js in the browser:

(function() {
  "something ������ something";


Whether set settings.default_encoding => 'utf-8' or leave it as default will see the same result.

If use coffee -c . in CMD directly, the output is good.

The same input will output good result in Slim, Sass, etc.

More information:

I found that only if i require "gravatar-ultimate" it will output such result, otherwise it will raise a error.

Simply run this:

# encoding: utf-8
require "sinatra/base"
require "coffee_script"
class CoffeeChs < Sinatra::Base
  get "/" do
    coffee :coffee_chs, :views => "."
  run! if app_file == $0


alert "中文"

Will raise Encoding::UndefinedConversionError - "\xE4" from ASCII-8BIT to UTF-8:

Find a similar issue?


Finally I add below in my own application to avoid this problem:

# encoding: utf-8

# Refer
module Tilt
  class CoffeeScriptTemplate < Template
    alias_method :original_prepare, :prepare

    def prepare
      @data.force_encoding 'UTF-8'
Sinatra member
rkh commented Aug 31, 2012

discuss here: rtomayko/tilt#75

@rkh rkh closed this Aug 31, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.