mysql,Linux,HighPerformance,ruby on Rails

2010年7月11日星期日

ruby1.9.1对String的处理有细微差别,注意

今天在ruby1.9.2-dev环境下起Oria,报了个错:
/Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `+': String can't be coerced into Fixnum (TypeError)
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `block in port'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `each'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `inject'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/lib/oria.rb:57:in `port'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/bin/oria:43:in `parse_options'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/bin/oria:49:in `start'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/bin/oria:11:in `go'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/oria-0.1.1/bin/oria:76:in `<top (required)>'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/bin/oria:19:in `load'
        from /Users/xiaodeshi/.rvm/gems/ruby-1.9.2-head@rails3beta/bin/oria:19:in `<main>'

心中奇怪,这昨天才用了一下没这问题啊。找开对应文件定位到:
  def self.port
    @@port ||= "Oria is easily the coolest in-memory, super-simple KVS. Cool people use it.".split("").inject(0) {|total, char| total += (char[0].class == String ? char[0].ord : char[0])}
  end

直接去irb下跑,原来作者是使用charcode加出来的一个端口,但1.9.2修正了1.8.7字符串的显示不一致问题。
原来在1.8.7下,'a'[0]出来的是ascii code,但在1.9.2下'a'[0]还是返回'a',这代码就跑不动了。
ok,了解原因后,改一行就ok了。
@@port ||= "Oria is easily the coolest in-memory, super-simple KVS. Cool people use it.".split("").inject(0) {|total, char|  total += (char[0].class == String ? char[0].ord : char[0])}

兼容1.8.7.


--
tommy xiao
E-mail: xiaods(AT)gmail.com

没有评论:

发表评论