0

i can't figure out what's wrong with my code, can you help me please?

this is constructor of my class:

def initialize(hash_table_size)
  @size = hash_table_size
  @table = Array.new(hash_table_size) { LinkedList.new }
end

this is method in that class:

def to_a
  arr = Array.new
  @table.each { |list| list.each { |o| arr << o } }
  arr
end

this is my "each" method in LinkedList class:

def each
  if @length > 0
    item = @head
    begin
      yield item.object
      item = item.next
    end until item.nil?
  end
end

and this is what i get from unittest:

1) Error:
test_initial_size_3(HashSetTest):
NoMethodError: undefined method `each' for 3:Fixnum
    C:/Users/Ajax/My Documents/Aptana Studio 3 Workspace/alg_du1_jan_svec/hash_set.rb:34:in `block in to_a'
    C:/Users/Ajax/My Documents/Aptana Studio 3 Workspace/alg_du1_jan_svec/hash_set.rb:34:in `each'
    C:/Users/Ajax/My Documents/Aptana Studio 3 Workspace/alg_du1_jan_svec/hash_set.rb:34:in `to_a'
    C:/Users/Ajax/My Documents/Aptana Studio 3 Workspace/alg_du1_jan_svec/hash_set_test.rb:14:in `test_initial_size_3'

1 tests, 3 assertions, 0 failures, 1 errors, 0 skips
4

1 回答 1

1

It means that LinkedList.new in the method initialize is returning 3, which becomes an element of @table, and is substituted into the block variable list of the method to_a.

于 2013-03-19T04:37:47.763 回答