In a very simple program trying to use “each” I get the error : sum.rb:16: undefined method `each’ for # (NoMethodError)
I get the same error using “each” various ways: the one shown below was copied directly from the Class:Matrix from ruby-doc.org.
# create State database using matrix
require 'matrix'
State=Matrix[ [3,1,4,4,6,2,8,12,6,2],
[6,2,4,13,25,21,11,22,9,3,],
[6,20,27,34,22,14,12,11,2,5],
[6,28,17,23,31,18,15,9,18,12],
[9,18,11,13,8,27,10,14,24,11],
[3,9,7,16,9,15,28,24,29,21],
[5,8,4,7,17,14,19,30,33,4],
[7,17,23,9,5,9,22,21,12,21,],
[7,14,25,22,16,10,19,15,12,11],
[5,16,7,3,6,3,9,8,1,5] ]
State.each { |e| puts e }
This must be simple/obvious but I can’t figure it out. (I’ve already done a variety of things with the State matrix that work out as expected.)
Ruby 1.8.7 will return the problem you are seeing. The Matrix object in 1.8.7 doesn’t have an
eachmethod.Upgrading to a recent version, either 1.9.2 or 1.9.3 will fix the problem.