Is there any gem which adds # encoding: UTF-8
to each Ruby file automatically?
是否有任何gem自动为每个Ruby文件添加#coding:UTF-8?
Or is there any other way to prevent from the invalid multibyte char (US-ASCII)
error in the entire Ruby on Rails project (not in a single class only)?
或者是否有任何其他方法可以防止整个Ruby on Rails项目中的无效多字节字符(US-ASCII)错误(仅限于单个类)?
6 个解决方案
#1
24
Try magic_encoding gem, it can insert uft-8 magic comment to all ruby files in your app.
尝试使用magic_encoding gem,它可以将uft-8魔法注释插入应用中的所有ruby文件。
[EDIT] Having switched to SublimeText now I use auto-encoding-for-ruby plugin.
[编辑]现在切换到SublimeText我使用auto-encoding-for-ruby插件。
#2
26
Upgrade to Ruby 2.0, as it makes UTF-8 the default encoding, removing the need for magic comments.
升级到Ruby 2.0,因为它使UTF-8成为默认编码,无需魔术评论。
#3
6
Vim:
Vim的:
:args **/*.ruby
:set hidden
:argdo norm! O# encoding: UTF-8
:wqa
#4
2
If you're using Sublime Text 2, you can use a plugin that automatically includes encoding declaration when needed: https://github.com/elomarns/auto-encoding-for-ruby.
如果您使用的是Sublime Text 2,则可以使用在需要时自动包含编码声明的插件:https://github.com/elomarns/auto-encoding-for-ruby。
#5
2
How about just running a script?
如何运行脚本呢?
#!/usr/bin/env ruby1.9.1
require 'find'
fixfile = []
Find.find('.') do |path|
next unless /\.rb$/.match(path);
File.open(path) do |file|
count = 0;
type = :lib
file.each do |line|
if count == 0 and /#!/.match(line)
type = :script
end
if /utf/.match(line)
break
end
if (count += 1) > 10 then
fixfile.push path:path, type:type
break
end
end
if file.eof?
fixfile.push path:path, type:type
end
end
end
fixfile.each do |info|
path = info[:path]
backuppath = path + '~'
type = info[:type]
begin
File.delete(backuppath) if File.exist?(backuppath)
File.link(path, backuppath)
rescue Errno::ENOENT => x
puts "could not make backup file '#{backuppath}' for '#{ path }': #{$!}"
raise
end
begin
inputfile = File.open(backuppath, 'r')
File.unlink(path)
File.open(path, 'w') do |outputfile|
if type == :script
line = inputfile.readline
outputfile.write line
end
outputfile.write "# encoding: utf-8\n"
inputfile.each do |line|
outputfile.write line
end
inputfile.close
outputfile.close
end
rescue => x
puts "error: #{x} #{$!}"
exit
end
To make it automatic add this to your Rakefile.
要使其自动添加到您的Rakefile。
You could run file -bi #{path}
and look for charset=utf-8 if you only want to update files that have utf-8 chars.
如果您只想更新具有utf-8字符的文件,则可以运行文件-bi#{path}并查找charset = utf-8。
#6
0
Adding a # encoding: UTF-8
to each Ruby file automatically makes only sense, when your files are really stored in UTF-8.
当您的文件真正存储在UTF-8中时,自动添加#coding:UTF-8到每个Ruby文件是有意义的。
If your files are encoded CP850 (AFAIK default in Windows) and you use Non-ASCII characters, you replace invalid multibyte char (US-ASCII)
with invalid multibyte char (UTF-8)
.
如果您的文件是CP850(Windows中的AFAIK默认值)编码而您使用的是非ASCII字符,则将无效的多字节字符(US-ASCII)替换为无效的多字节字符(UTF-8)。
I would prefer a manual modification and check of each file, if it is really UTF-8.
我更喜欢手动修改和检查每个文件,如果它真的是UTF-8。
#1
24
Try magic_encoding gem, it can insert uft-8 magic comment to all ruby files in your app.
尝试使用magic_encoding gem,它可以将uft-8魔法注释插入应用中的所有ruby文件。
[EDIT] Having switched to SublimeText now I use auto-encoding-for-ruby plugin.
[编辑]现在切换到SublimeText我使用auto-encoding-for-ruby插件。
#2
26
Upgrade to Ruby 2.0, as it makes UTF-8 the default encoding, removing the need for magic comments.
升级到Ruby 2.0,因为它使UTF-8成为默认编码,无需魔术评论。
#3
6
Vim:
Vim的:
:args **/*.ruby
:set hidden
:argdo norm! O# encoding: UTF-8
:wqa
#4
2
If you're using Sublime Text 2, you can use a plugin that automatically includes encoding declaration when needed: https://github.com/elomarns/auto-encoding-for-ruby.
如果您使用的是Sublime Text 2,则可以使用在需要时自动包含编码声明的插件:https://github.com/elomarns/auto-encoding-for-ruby。
#5
2
How about just running a script?
如何运行脚本呢?
#!/usr/bin/env ruby1.9.1
require 'find'
fixfile = []
Find.find('.') do |path|
next unless /\.rb$/.match(path);
File.open(path) do |file|
count = 0;
type = :lib
file.each do |line|
if count == 0 and /#!/.match(line)
type = :script
end
if /utf/.match(line)
break
end
if (count += 1) > 10 then
fixfile.push path:path, type:type
break
end
end
if file.eof?
fixfile.push path:path, type:type
end
end
end
fixfile.each do |info|
path = info[:path]
backuppath = path + '~'
type = info[:type]
begin
File.delete(backuppath) if File.exist?(backuppath)
File.link(path, backuppath)
rescue Errno::ENOENT => x
puts "could not make backup file '#{backuppath}' for '#{ path }': #{$!}"
raise
end
begin
inputfile = File.open(backuppath, 'r')
File.unlink(path)
File.open(path, 'w') do |outputfile|
if type == :script
line = inputfile.readline
outputfile.write line
end
outputfile.write "# encoding: utf-8\n"
inputfile.each do |line|
outputfile.write line
end
inputfile.close
outputfile.close
end
rescue => x
puts "error: #{x} #{$!}"
exit
end
To make it automatic add this to your Rakefile.
要使其自动添加到您的Rakefile。
You could run file -bi #{path}
and look for charset=utf-8 if you only want to update files that have utf-8 chars.
如果您只想更新具有utf-8字符的文件,则可以运行文件-bi#{path}并查找charset = utf-8。
#6
0
Adding a # encoding: UTF-8
to each Ruby file automatically makes only sense, when your files are really stored in UTF-8.
当您的文件真正存储在UTF-8中时,自动添加#coding:UTF-8到每个Ruby文件是有意义的。
If your files are encoded CP850 (AFAIK default in Windows) and you use Non-ASCII characters, you replace invalid multibyte char (US-ASCII)
with invalid multibyte char (UTF-8)
.
如果您的文件是CP850(Windows中的AFAIK默认值)编码而您使用的是非ASCII字符,则将无效的多字节字符(US-ASCII)替换为无效的多字节字符(UTF-8)。
I would prefer a manual modification and check of each file, if it is really UTF-8.
我更喜欢手动修改和检查每个文件,如果它真的是UTF-8。