ruby – Vpim :: Vcard – 解析vcard 2.1
我的代码必须解析Vcard 2.1格式.
我正在使用vpim(没有其他库) 当我运行Vpim :: Vcard.decode(数据)时,我收到错误: undefined method `each' for #<String:0x0000000928e778> 堆栈跟踪: NoMethodError (undefined method `each' for #<String:0x0000000928e778>): vpim (0.695) lib/vpim/rfc2425.rb:82:in `unfold' vpim (0.695) lib/vpim/rfc2425.rb:308:in `decode' vpim (0.695) lib/vpim/vcard.rb:692:in `decode' app/models/event.rb:71:in `block (2 levels) in parse_data' 我尝试运行gem install vcard并在RailsApp初始化后要求’vcard’directctry(在vpim gem之后config.gem不包含此文件).(config.gem’vpim’包含在environment.rb中) = D0 = 9D = D0 = B0 = D0 = B4 = D1 = 80 = D0 = B0 = 20 = D0 = B1 = D0 = B0 = D0 = BD = D0 = BA = 20 = D0 = BE = D0 = BB = (异常类是Vpim :: InvalidEncodingError) Vcard代码我尝试解析: BEGIN:VCARD VERSION:2.1 REV:20090710T151929Z TEL;CELL:80954130722 X-CLASS:private END:VCARD 有趣的是,第二个错误是我在Rails模型中解码时. 堆栈跟踪: Vpim::InvalidEncodingError (;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA=20=D0=BE=D0=BB=): vcard (0.1.1) lib/vcard/field.rb:106:in `decode0' vcard (0.1.1) lib/vcard/field.rb:172:in `initialize' vcard (0.1.1) lib/vcard/field.rb:183:in `new' vcard (0.1.1) lib/vcard/field.rb:183:in `decode' vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `block in decode' vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `collect' vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `decode' vcard (0.1.1) lib/vcard/vcard.rb:686:in `decode' app/models/event.rb:71:in `block (2 levels) in parse_data' app / models / event.rb:71: vcard = Vpim::Vcard.decode(contact.text) 以下是irb的例子: 95-25-164-74:~ smix$irb ruby-1.9.2-rc2 > str = <<EOS ruby-1.9.2-rc2"> BEGIN:VCARD ruby-1.9.2-rc2"> VERSION:2.1 ruby-1.9.2-rc2"> REV:20090710T151929Z ruby-1.9.2-rc2"> TEL;CELL:80954130722 ruby-1.9.2-rc2"> X-CLASS:private ruby-1.9.2-rc2"> END:VCARD ruby-1.9.2-rc2"> EOS => "BEGIN:VCARDnVERSION:2.1nREV:20090710T151929ZnTEL;CELL:80954130722nX-CLASS:privatenEND:VCARDn" ruby-1.9.2-rc2 > require 'vpim' => true ruby-1.9.2-rc2 > Vpim::Vcard.decode str NoMethodError: undefined method `each' for #<String:0x000001010e0428> from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:82:in `unfold' from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:308:in `decode' from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/vcard.rb:692:in `decode' from (irb):10 from /Users/smix/.rvm/rubies/ruby-1.9.2-rc2/bin/irb:17:in `<main>' ruby-1.9.2-rc2 > 如何在rails中解析Vcard 2.1? 解决方法
问题是在Ruby 1.9.1中删除了String#. (它的功能在String#each_line中持久).要修复/解决此错误,请执行以下任一操作:
>在Ruby 1.8.7上运行rails应用程序. class String alias_method :each,:each_line end >切换到vcard gem,它取自Vpim并且支持Ruby 1.9.1. 我推荐第4个选项. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |