Question

I am trying to manage a 2D map of homemade objects (called CaseSimple). This 2D map is another homemade object, here called MapSimple.

I try to code that. And I have a problem about CaseSimple coordinates (the only attributes of this class) and the value it take.

# simple container of coordinates X and Y
class CaseSimple
    @coordx
    @coordy
    attr_reader :coordx, :coordy

    def initialize(x, y)
        @coordx, @coordy = x, y
        #print "\n\nget : ", @coordx, ";", @coordy,"\n"
    end
end



# method for return printable string that represent the given map of CaseSimple
def printableMap(map)
    s = ""
    0.upto(9) do |j|
        0.upto(9) do |i|
            s += "(" + map[i][j].coordx.to_s + ";" + map[i][j].coordy.to_s + ") "
        end
        s +=  "\n"
    end
    s +=  "\n"
    return s
end




# 2D map of CaseSimple
class MapSimple
    @cases

    def initialize
        @cases = Array.new(10, Array.new(10,nil))
        # creat & print without error
        print "PRINT AND CREAT :\n"
        0.upto(9) do |j|
            0.upto(9) do |i|
                @cases[i][j] = CaseSimple.new(i,j)
                print "(", @cases[i][j].coordx, ";", @cases[i][j].coordy, ") "
            end
            print "\n"
        end
        print "\n"

        # print cases with printableMap
        print "PRINT WITH EXTERN FUNCTION :\n"
        print printableMap(@cases) # ERROR

        # or without creating CaseSimple
        print "PRINT WITHOUT CREAT :\n"
        0.upto(9) do |j|
            0.upto(9) do |i|
                print "(", @cases[i][j].coordx, ";", @cases[i][j].coordy, ") "
            end
            print "\n"
        end
        print "\n"


    end

    # print cases
    def to_s()
        return printableMap(@cases) 
    end


end




d = MapSimple.new
print "PRINT BY TO_S METHOD :\n"
print d

And, i use irb (ruby 1.9.3) on this code.

PRINT AND CREAT :
(0;0) (1;0) (2;0) (3;0) (4;0) (5;0) (6;0) (7;0) (8;0) (9;0) 
(0;1) (1;1) (2;1) (3;1) (4;1) (5;1) (6;1) (7;1) (8;1) (9;1) 
(0;2) (1;2) (2;2) (3;2) (4;2) (5;2) (6;2) (7;2) (8;2) (9;2) 
(0;3) (1;3) (2;3) (3;3) (4;3) (5;3) (6;3) (7;3) (8;3) (9;3) 
(0;4) (1;4) (2;4) (3;4) (4;4) (5;4) (6;4) (7;4) (8;4) (9;4) 
(0;5) (1;5) (2;5) (3;5) (4;5) (5;5) (6;5) (7;5) (8;5) (9;5) 
(0;6) (1;6) (2;6) (3;6) (4;6) (5;6) (6;6) (7;6) (8;6) (9;6) 
(0;7) (1;7) (2;7) (3;7) (4;7) (5;7) (6;7) (7;7) (8;7) (9;7) 
(0;8) (1;8) (2;8) (3;8) (4;8) (5;8) (6;8) (7;8) (8;8) (9;8) 
(0;9) (1;9) (2;9) (3;9) (4;9) (5;9) (6;9) (7;9) (8;9) (9;9) 

PRINT WITH EXTERN FUNCTION :
(9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) 
(9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) 
(9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) 
(9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) 
(9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) 
(9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) 
(9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) 
(9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) 
(9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) 
(9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) 

PRINT WITHOUT CREAT :
(9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) 
(9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) 
(9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) 
(9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) 
(9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) 
(9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) 
(9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) 
(9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) 
(9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) 
(9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) 

=> (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) 
(9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) 
(9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) 
(9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) 
(9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) 
(9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) 
(9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) 
(9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) 
(9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) 
(9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) 


TestDamier.rb(main):078:0> print "PRINT BY TO_S METHOD :\n"
PRINT BY TO_S METHOD :
=> nil
TestDamier.rb(main):079:0> print d
(9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) (9;0) 
(9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) (9;1) 
(9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) (9;2) 
(9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) (9;3) 
(9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) (9;4) 
(9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) (9;5) 
(9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) (9;6) 
(9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) (9;7) 
(9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) (9;8) 
(9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) (9;9) 

=> nil

I think its anormal : i want to have a map of CaseSimple where each CaseSimple know its own coordinates.

I work under Debian 7, and irb -v return me irb 0.9.6(09/06/30).

Thank you for response ! (and sorry for my english)

Was it helpful?

Solution

Your main problem is that the following code:

@cases = Array.new(10, Array.new(10,nil))

creates an array where all the top level elements/rows are the same, so as you iterate through the top level array, the values you assign to the last row/element are what appear on all rows/elements.

You need to use:

@cases = Array.new(10) { Array.new(10) }

This is discussed in http://ruby-doc.org/core-2.0.0/Array.html#class-Array-label-Creating+Arrays.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top