带有特殊字符的 Ruby 1.9

标签 ruby string ruby-1.9

我有很多这样的代码行:

 @breadcrumb = []
@breadcrumb << ["#!", "Hladať"]

它是在 ruby​​ ree-1.8 中,但我可以将其更改为 1.9,但出现此错误:

/app/controllers/index_controller.rb:36: syntax error, unexpected $end, expecting ']'
@breadcrumb << ["#!", "Hladať"]

当我删除“ť”和其他特殊字符(ľščťžýáí...)时,没关系,但我需要这些字符。

最佳答案

将指定编码的“神奇注释”添加到每个包含非 ASCII 字符的 Ruby 文件的顶部:

# encoding: UTF-8

如果 config.encoding 设置正确(默认为 UTF-8),Rails View 文件中不需要这样做。您还应该阅读有关 Ruby 1.9's encoding behavior 的更多信息.

关于带有特殊字符的 Ruby 1.9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10483507/

相关文章:

ruby - 不转换成字符串,一个Fixnum有多少位数?

ruby-on-rails - rails/设计 : undefined method `router_name' for nil:NilClass - what is coming through as nil?

ruby - 如何摆脱 ruby​​ 的警告 : already initialized constant

ruby - 如何在 Ruby 中原生实现像 [] 这样的方法?

ruby - Time.to_i 是否总是以 UTC 返回自 EPOCH 以来的秒数?

ruby - 乘号:* do?是什么意思

ruby-on-rails - 带有 rails 的欧洲电话格​​式

python - 在 Python 中使用函数作为 re.sub 的参数?

c# - 搜索百分比字符串

java - Android 将字符串转换为 ObjectInputStream