newbie, to_s question

E

EdUarDo

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
#<Tactic:0xb78db8fc>


Why do I get the internal representation of Tactic?

Why is not the response '4-4-2' only?

4-4-2
 
R

Robert Klemme

EdUarDo said:
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
#<Tactic:0xb78db8fc>


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
 
G

Gavin Kistner

--Apple-Mail-3--136124722
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed

def to_s
puts "#@name"
end

Change that to:
def to_s
"#@name"
end

The #to_s method should return the string representation, not output it.


--Apple-Mail-3--136124722--
 
E

EdUarDo

Because your to_s method returns nil. You want this
def to_s() @name.to_s end

and not

That's right, I forgot that to_s must return a string, not to print a string
I'd also use the block form:

File.open('tactics.dat') do |file|
# read
end

I'll do, thanks a lot.
 
N

nobu.nokada

Hi,

At Tue, 16 Aug 2005 22:36:14 +0900,
EdUarDo wrote in [ruby-talk:152360]:
def to_s
puts "#@name"
end

You don't return a String, but nil.
Why do I get the internal representation of Tactic?

`puts' converts the arguments by calling `to_s', but use
default `to_s' if it didn't return a String.
 
C

Charles Steinman

EdUarDo said:
def to_s
puts "#@name"
end
end


I do:

tactic = Tactic.new
tactic.loadDefaultTactic
puts tactic

and get:

4-4-2
#<Tactic:0xb78db8fc>


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:

def to_s
@name.to_s
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top