ruby syntax error with multiple language in hash

喜你入骨 提交于 2019-12-12 01:33:11

问题


The following hash is causing a syntax error. I assume it is because of the funky characters.

Any way to fix this? I'm using macvim, in case that matters.

  {
    :en => ['English',  'en_US'], 
    :es => ['español',  'es_MX'],
    :fr => ['français', 'fr_FR'],
    :de => ['Deutsch',  'de_DE'],
    :ru => ['русский',  'ru_RU'],
    :zh => ['中国的',   'zh_CN'],
    :ar => ['العربية',   'ar_AR'],
  }

回答1:


If this is Ruby 1.9, then you can set the magic comment to tell Ruby this is a UTF8 file instead of ASCII:

How does the magic comment ( # Encoding: utf-8 ) in ruby​​ work?




回答2:


You could always escape your unicode values.

{
  :en => ['English',  'en_US'], 
  :es => ['espa\u00F1ol',  'es_MX'],
  :fr => ['fran\u00E7ais', 'fr_FR'],
  :de => ['Deutsch',  'de_DE'],
  :ru => ['\u0440\u0443\u0441\u0441\u043A\u0438\u0439',  'ru_RU'],
  :zh => ['\u4E2D\u56FD\u7684',   'zh_CN'],
  :ar => ['\u0627\u0644\u0639\u0631\u0628\u064A\u0629',   'ar_AR'],
}


来源:https://stackoverflow.com/questions/14495486/ruby-syntax-error-with-multiple-language-in-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!