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 / 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/.
>