Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruport - undefined method `each' for "":String

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?

like image 572
el_quick Avatar asked Aug 09 '26 05:08

el_quick


1 Answers

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.

like image 159
knut Avatar answered Aug 11 '26 19:08

knut