Ruport - undefined method `each' for “”:String

≡放荡痞女 提交于 2019-12-11 08:56:59

问题


I am following the first example in this tutorial: http://ruportbook.com/printable_documents.html, but I'm getting undefined method `each' for "":String all the time, I created a new file with this code:

class MultiTableController < Ruport::Controller    
  stage :multi_table_report    
  class PDF < Ruport::Formatter::PDF    
    renders :pdf, :for => MultiTableController    
    build :multi_table_report do
      data.each { |table| pad(10) { draw_table(table) } }
      render_pdf
    end    
  end    
end

Then, in an existing controller named workers_controller.rb I have the next action:

  def index_report
    t1 = Table(%w[a b c]) << [1,2,3] << [4,5,6]
    t2 = Table(%w[a b c]) << [7,8,9] << [10,11,12]
    pdf = MultiTableController.render_pdf(:data => [t1,t2])
  end

Then, I'm getting this error on my browser:

undefined method `each' for "1":String

I have tried many others examples and I get the same error.

Some help?


回答1:


eachwas a method of String in ruby 1.8 and it was removed in Ruby 1.9.

The reason was Unicode, or better the new encoding possibilities of ruby 1.9.

What should String #each do? Loop on each byte or each character? Ruby can't decide it for you, so you have to use String#each_byte or String#each_char.

In Ruby 1.8 it was no difference, a character was a byte.


Edit:

Just give a dirty hack a chance:

class String
  alias :each :each_char
end

'aaaa'.each{|x| p x }

But ruport seems to have other problems with Ruby 1.9 and there may be side effects. I wouldn't recommend this hack in a bigger project, but maybe it works in small scripts.




回答2:


String.each in 1.8.7 works with lines, not with bytes or chars. In 1.9 use String.split("\n").each instead of String.each. But be careful, split cut off delimiter, so String.each give string ended with "/n", when String.split("\n").each give string without "\n" on end.



来源:https://stackoverflow.com/questions/10788770/ruport-undefined-method-each-for-string

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