2

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?

el_quick
  • 4,656
  • 11
  • 45
  • 53
  • Can you show in what string error heppens? Doesn't looks like this code directly uses `each` on `String` – Flexoid May 28 '12 at 18:13
  • As you can see above, the error is happens at "1" string, I changed t1 = Table(%w[a b c]) << [1,2,3] << [4,5,6] to t1 = Table(%w[a b c]) << [[1],[2],[3]] << [[4],[5],[6]] to get some another clue, but the error now is: undefined method `each' for "[1]":String :/ – el_quick May 28 '12 at 18:16

2 Answers2

10

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.

knut
  • 27,320
  • 6
  • 84
  • 112
  • Sorry, I don't know Ruport. But there is a report on [missing ruby 1.9.2 support](https://github.com/ruport/ruport/issues/14), and on [ruby talk}(http://ruby.11.n6.nabble.com/ruport-in-ruby1-9-td3381908.html) I found _Ruport 1.x will never be Ruby 1.9 compatible unless someone else does the work, but Ruport 2 will be_ - But I found no Ruport 2. See also my changed answer. – knut May 28 '12 at 18:26
  • Hello, the hack works good, but I get other error now: "no marshal_dump is defined for class Mutex" I think that is better search another option for my reports, thk for all...! – el_quick May 28 '12 at 18:42
-1

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.

Pavel Vlaskin
  • 91
  • 1
  • 7