class Tactic
attr_reader :name, :cells
class Cell
attr_reader :x, :y, :position, :playerId
attr_writer :x, :y, :position, :playerId
def initialize(x, y, position) @x = x @y = y @playerId = 0 @position = position
end
def to_s
puts "(#@x, #@y): #@position, #@playerId"
end
end
def loadDefaultTactic
file = File.new('tactics.dat')
# Identificador de la tactica @name = file.readline
# Posiciones
file.each(';') {
>line> coords = line.split(',')
x, y = coords[0].to_i, coords[1].to_i @cells << Cell.new(x, y, getPosition(x, y))
}
file.close
end
def initialize @cells = Array.new
# TODO Hay que leer el fichero de datos y cargar la tactica por defecto
end
class Tactic
attr_reader :name, :cells
class Cell
attr_reader :x, :y, :position, :playerId
attr_writer :x, :y, :position, :playerId
def initialize(x, y, position) @x = x @y = y @playerId = 0 @position = position
end
def to_s
puts "(#@x, #@y): #@position, #@playerId"
end
end
def loadDefaultTactic
file = File.new('tactics.dat')
# Identificador de la tactica @name = file.readline
# Posiciones
file.each(';') {
>line> coords = line.split(',')
x, y = coords[0].to_i, coords[1].to_i @cells << Cell.new(x, y, getPosition(x, y))
}
file.close
end
def initialize @cells = Array.new
# TODO Hay que leer el fichero de datos y cargar la tactica
por defecto end
Why do I get the internal representation of Tactic?
Why is not the response '4-4-2' only?
You are not returning "4-4-2" in to_s. You are directly printing
"4-4-2" with puts in the to_s method. The return value of puts is nil.
So to_s is printing "4-4-2" and then returning nil. And when to_s
returns nil, puts will instead use inspect to get a string for output.
So the second puts prints tactic.inspect.
You want your to_s method to look something like this: