From: Eduardo Rocha on
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: myemail(a)this.com
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!!!
--
Posted via http://www.ruby-forum.com/.

From: Eduardo Rocha on
up?
--
Posted via http://www.ruby-forum.com/.

From: Gennady Bystritsky on

On Dec 23, 2009, at 3:24 PM, Eduardo Rocha wrote:

> 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, remove puts() from to_s() and simply return a string. In your case, return from to_s() is the content of your @persons array, it gets printed by your "puts 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.

> 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: myemail(a)this.com
> 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!!!
> --
> Posted via http://www.ruby-forum.com/.
>


From: Marnen Laibow-Koser on
Eduardo Rocha wrote:
> up?

Be patient.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen(a)marnen.org
--
Posted via http://www.ruby-forum.com/.

From: Eduardo Rocha on
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
--
Posted via http://www.ruby-forum.com/.