# NoMethodError?

**URL:** <https://www.ruby-forum.com/t/nomethoderror/147178>\
**Category:** Ruby\
**Created:** [August 29, 2008, 3:34am UTC](https://www.ruby-forum.com/t/nomethoderror/147178 "2008-08-29T03:34:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [August 29, 2008, 3:34am UTC](https://www.ruby-forum.com/t/nomethoderror/147178/1 "2008-08-29T03:34:15Z")

</div>

In the following code I am trying to print out the value of the  
variable @symbol from an array of instances of the MapSector class. I  
don’t understand why I’m getting “NoMethodError: undefined method  
`symbol’ for #Array:0xb7cb025c” after putting this in irb and typing:  
foo = GameMap.new(5,5)  
foo.show\_map

class GameMap  
attr\_accessor :height, :width, :id, :sectors  
def initialize height, width  
@height = height  
@width = width  
@id = nil  
@sectors = Array.new(@height, MapSector.new) {Array.new(@width,  
MapSector.new)}  
end

def show\_map  
@sectors.each do |x|  
puts x.symbol  
end  
end

end

class MapSector  
attr\_accessor :type, :symbol, :players  
def initialize  
@type = “plains”  
@players = []  
@symbol = “.”  
end  
end

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [August 29, 2008, 4:24am UTC](https://www.ruby-forum.com/t/nomethoderror/147178/2 "2008-08-29T04:24:11Z")

</div>

Chris B. wrote:

> In the following code I am trying to print out the value of the  
> variable @symbol from an array of instances of the MapSector class. I  
> don’t understand why I’m getting “NoMethodError: undefined method  
> `symbol’ for #Array:0xb7cb025c” after putting this in irb and typing:  
> foo = GameMap.new(5,5)  
> foo.show\_map

Well I figured out the first half of my problem. I guess my show\_map  
method was trying to find the symbol variable inside the Array object,  
whoops. So I have updated the show\_map method to the following.

def show\_map  
x = 0  
y = 0  
while x \< @height  
while y \< @width  
puts self.sectors[x][y].symbol  
y+= 1  
end  
x += 1  
end  
end

The problem I am having now is that when the method is run on a 4x4  
array of MapSectors it only prints out 4 symbols…it should be 16. I  
have verified that the nested arrays are all properly populated so I  
guess it just isn’t iterating through them properly. Any ideas?

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [August 29, 2008, 4:48am UTC](https://www.ruby-forum.com/t/nomethoderror/147178/3 "2008-08-29T04:48:15Z")

</div>

> The problem I am having now is that when the method is run on a 4x4  
> array of MapSectors it only prints out 4 symbols…it should be 16. I  
> have verified that the nested arrays are all properly populated so I  
> guess it just isn’t iterating through them properly. Any ideas?

Ok I’ve gotten it all figured out and working, thank’s anyhow! =)
