Puts return

E

Eduardo Rocha

Hello guys, i'm new in ruby and i'm trying to create an Address Book.
(I'm from Brasil)
When i put the book to print
"puts lista" #lista is the Address Book
the method to_s of the AddresBook class is like this:
"def to_s
puts "Lista Telefonica"
puts "Numero de Contatos: " + @qtContatos.to_s
@persons.each do |pessoa|
#persons is an array of a Class Person that have it's own to_s method
puts pessoa
puts ""
end
end
"

It's working well, but when I run it in the cmd:
"C:\Users\Dudu\.gem\exe>ruby ListaTelefonica.rb"
(I'm using Windows 7 with Gems version "1.3.5")

The print Returns the & (address) of each thing I print...
example:
"
Nome: My Name
Email: (e-mail address removed)
Fone: XXXXXXX
Endereco:
Rua NumSei, 321
Cidade: Anyone
Pais: Brasil
#<Address:0x2803ae0>
#<Pessoa:0x2803b20>

#<AddressBook:0x28043a0>
"

How can I take this Address out??
this "#<AddressBook:0x28043a0>" and the others
:S
Thanks!!!
 
G

Gennady Bystritsky

Hello guys, i'm new in ruby and i'm trying to create an Address Book.
(I'm from Brasil)
When i put the book to print
"puts lista" #lista is the Address Book
the method to_s of the AddresBook class is like this:
"def to_s
puts "Lista Telefonica"
puts "Numero de Contatos: " + @qtContatos.to_s
@persons.each do |pessoa|
#persons is an array of a Class Person that have it's own to_s method
puts pessoa
puts ""
end

First of all, method to_s() should *return* a string, not print it. So, rem=
ove puts() from to_s() and simply return a string. In your case, return fro=
m to_s() is the content of your @persons array, it gets printed by your "pu=
ts lista" statement.

You can do something like the following:

def to_s
[
"Lista Telefonica",
"Numero de Contatos: #{@qtContatos}",
@persons.map { |pessoa|
pessoa.to_s
}
].join("\n")
end

Then, if to_s() for objects in your @persons array is defined accordingly, =
you should have a nice looking output.

Gennady.
 
E

Eduardo Rocha

Thanks for the help =D
And i'll try to understand how this works... couse i'm a beginner in
ruby! hehehe
Bye!
def to_s
[
"Lista Telefonica",
"Numero de Contatos: #{@qtContatos}",
@persons.map { |pessoa|
pessoa.to_s
}
].join("\n")
end
 
S

Saji Jaooon

Eduardo said:
Thanks for the help =D
And i'll try to understand how this works... couse i'm a beginner in
ruby! hehehe
Bye!
def to_s
[
"Lista Telefonica",
"Numero de Contatos: #{@qtContatos}",
@persons.map { |pessoa|
pessoa.to_s
}
].join("\n")
end

class x
def y(@y1,@y2)
end

def to_s
@y1
@y2
end
end
 
E

Eduardo Rocha

Thanks to u all =D
i'll read a bit more about ruby to undersand everything
Hugs!
 

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
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top