0

I am trying to build custom string based on data from my class Map.

class Map
  def initialize
    @data = Array.new(3) { Array.new(3, 0) }
  end

  def [](x, y)
    @data[x][y]
  end

  def []=(x, y, value)
    @data[x][y] = value
  end
end

This is how I try to build a string:

def build_map_str(map)
  str = ' '
  (0..2).each do |i|
    (0..2).each do |j|
      str += case map[i][j]
             when 0
               "◻️ "
             when 1
               "❎ "
             when 2
               "🅾️ "
             end
      str += '| ' if i != 2
    end
    str += "\n"
  end
end

And this is how I call this func:

map = Map.new
str = build_map_str(map)

But I am getting in `[]': wrong number of arguments (given 1, expected 2). How could I fix it?

2 Answers 2

2

Given your operator

  def [](x, y)
    @data[x][y]
  end

Your map's elements should be accessed via map[i,j] instead of map[i][j].

Sign up to request clarification or add additional context in comments.

Comments

0

Your definition for Map#[] takes two arguments, so when you call it in build_map_str, it should look like this:

str += case map[i, j]

The same will apply when you assign values:

map[0, 0] = 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.