Ruby错误无效的多字节字符(US-ASCII)

时间:2022-07-16 09:44:52

I am trying to run the ruby script found here

我正在尝试运行这里找到的ruby脚本

but I am getting the error

但是我收到了错误

invalid multibyte char (US-ASCII)

for line 12 which is

第12行是

http = Net::HTTP.new("twitter.com", Net::HTTP.https_default_port())

can someone please explain to me what this means and how I can fix it, thanks

有人可以向我解释这意味着什么以及如何解决它,谢谢

2 个解决方案

#1


12  

When you run the script with Ruby 1.9, change the first two lines of the script to:

使用Ruby 1.9运行脚本时,将脚本的前两行更改为:

#!/usr/bin/env ruby
# encoding: utf-8
require 'net/http'

This tells Ruby to run the script with support for the UTF-8 character set. Without that line Ruby 1.9 would default to the US_ASCII character set.

这告诉Ruby运行支持UTF-8字符集的脚本。如果没有该行,Ruby 1.9将默认为US_ASCII字符集。

Just for the record: This will not work in Ruby 1.8, because 1.8 doesn't knew anything about string encodings. And the line is not needed anymore in Ruby 2.0, because Ruby 2.0 is using UTF-8 as the default anyway.

仅供记录:这在Ruby 1.8中不起作用,因为1.8对字符串编码一无所知。而且在Ruby 2.0中不再需要该行,因为Ruby 2.0无论如何都使用UTF-8作为默认值。

#2


3  

It means that a multibyte character is used and Ruby is not set to handle it. If you are using an old version of Ruby, then put the following magic comment at the beginning of the file:

这意味着使用了多字节字符,而Ruby未设置为处理它。如果您使用的是旧版本的Ruby,请在文件开头添加以下魔术注释:

# coding: utf-8

If you use a modern version of Ruby, then that problem would not arise in the first place.

如果你使用现代版本的Ruby,那么首先不会出现这个问题。

#1


12  

When you run the script with Ruby 1.9, change the first two lines of the script to:

使用Ruby 1.9运行脚本时,将脚本的前两行更改为:

#!/usr/bin/env ruby
# encoding: utf-8
require 'net/http'

This tells Ruby to run the script with support for the UTF-8 character set. Without that line Ruby 1.9 would default to the US_ASCII character set.

这告诉Ruby运行支持UTF-8字符集的脚本。如果没有该行,Ruby 1.9将默认为US_ASCII字符集。

Just for the record: This will not work in Ruby 1.8, because 1.8 doesn't knew anything about string encodings. And the line is not needed anymore in Ruby 2.0, because Ruby 2.0 is using UTF-8 as the default anyway.

仅供记录:这在Ruby 1.8中不起作用,因为1.8对字符串编码一无所知。而且在Ruby 2.0中不再需要该行,因为Ruby 2.0无论如何都使用UTF-8作为默认值。

#2


3  

It means that a multibyte character is used and Ruby is not set to handle it. If you are using an old version of Ruby, then put the following magic comment at the beginning of the file:

这意味着使用了多字节字符,而Ruby未设置为处理它。如果您使用的是旧版本的Ruby,请在文件开头添加以下魔术注释:

# coding: utf-8

If you use a modern version of Ruby, then that problem would not arise in the first place.

如果你使用现代版本的Ruby,那么首先不会出现这个问题。