Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incompatible character encodings: ASCII-8BIT and UTF-8 Error #191

Open
wbzyl opened this issue Nov 2, 2011 · 6 comments
Open

incompatible character encodings: ASCII-8BIT and UTF-8 Error #191

wbzyl opened this issue Nov 2, 2011 · 6 comments

Comments

@wbzyl
Copy link

wbzyl commented Nov 2, 2011

Running:

thor test:example Þórr

on README with the content

Ł -- utf-8 encoded L slash
use rake

yields

You supplied the text: Þórr
    gsub  README
    .rvm/gems/ruby-1.9.2-p290/gems/thor-0.14.6/lib/thor/actions/file_manipulation.rb:227:in `gsub!': 
        incompatible character encodings: ASCII-8BIT and UTF-8 (Encoding::CompatibilityError)

Responsible for this is error the file test.thor:

require 'thor'

class Test < Thor
  include Thor::Actions

  desc "example TEXT", "an example task that replaces word 'rake' in README with provided TEXT"
  def example(text)
    puts "You supplied the text: #{text}"
    gsub_file 'README', /rake/ do
      "No more rake. Use #{text}"
    end
  end
end

The above means that I can't gsub a chunk of text with the Polish diacritics in a html.erb file, for example.

@znz
Copy link

znz commented Mar 8, 2013

I met same problem with rails template applications.
https://gist.github.com/znz/5117644

@aliciacatalina
Copy link

How did you fix it?

@znz
Copy link

znz commented Feb 28, 2014

I used

 content = File.read(path, encoding: 'utf-8')
 content.gsub! /"ほげ"/, '"ふが"'
 File.open(path, 'wb') { |file| file.write(content) }

instead of

 gsub_file "config/locales/hoge.yml", /"ほげ"/, '"ふが"'

@bastengao
Copy link

I met same problem using append_to_file, it seems File.binread method return 'ASCII-8BIT' encoding string.

see: https://github.com/erikhuda/thor/blob/master/lib/thor/actions/inject_into_file.rb#L98

@Fivell
Copy link

Fivell commented Dec 14, 2016

@bastengao , did you fix this somehow ?

@jeanfontaine
Copy link

jeanfontaine commented May 26, 2017

On Version 0.19.4 - November 28, 2016
rvm/gems/ruby-2.2.1/gems/thor-0.19.4/lib/thor/actions
0.19.4/lib/thor/actions/inject_into_file.rb:95
content = File.binread(destination).force_encoding("utf-8") # jjff 2017-05-26
helped for me. It was trial and error, not knowing ruby and gems.
Seems to me that as general rule for textfiles, binread should always be avoided as utf-8 is certainly byte-oriented, but definitely not a byte-coding (character-points can span over several bytes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants