From: Robert Klemme Date: 2005-08-16T22:46:14+09:00 Subject: Re: newbie, to_s question EdUarDo wrote: > Hi all, I have this class: > > 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 > > def to_s > puts "#@name" > end > end > > > I do: > > tactic = Tactic.new > tactic.loadDefaultTactic > puts tactic > > and get: > > 4-4-2 > # > > > Why do I get the internal representation of Tactic? Because your to_s method returns nil. You want this def to_s() @name.to_s end and not def to_s() puts "#@name" end > Why is not the response '4-4-2' only? Maybe you have an error in the reading code (file.each for example). I'd also use the block form: File.open('tactics.dat') do |file| # read end Kind regards robert