# Definning "to\_s" in a class - inexpected result

**URL:** <https://www.ruby-forum.com/t/definning-to-s-in-a-class-inexpected-result/140487>\
**Category:** Ruby\
**Created:** [June 14, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/definning-to-s-in-a-class-inexpected-result/140487 "2008-06-14T21:02:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaizen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4af34b/32.png) [@kaizen](https://www.ruby-forum.com/u/kaizen)\
**Post date:** [June 14, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/definning-to-s-in-a-class-inexpected-result/140487/1 "2008-06-14T21:02:26Z")

</div>

Hi,

I’m trying to obtain the cards of a Deck in a string like  
“2s…KsAs2h…Ah2d…Ad2c…Ac”. The problem with the folowing code is  
that in the end of the string appears “#Deck:0x2bb5880”. Why? Can I  
avoid it?

class Deck

SUITS = %w{s d h c}  
RANKS = %w{2 3 4 5 6 7 8 9 T J Q K A}

def initialize  
@cards = []  
SUITS.each{ |s| RANKS.each{ |r| @cards \<\< r+s } }  
end

def popCard  
@cards.pop  
end

def to\_s  
@cards.map{ |card| print card}  
end

end

d0 = Deck.new  
puts d0

Regards,  
AndrÃ©s

---

<div class="post-metadata">

**Author:** ![kaizen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4af34b/32.png) [@kaizen](https://www.ruby-forum.com/u/kaizen)\
**Post date:** [June 14, 2008, 9:08pm UTC](https://www.ruby-forum.com/t/definning-to-s-in-a-class-inexpected-result/140487/2 "2008-06-14T21:08:43Z")

</div>

AndrÃ©s SuÃ¡rez wrote:

> def to\_s  
> Â Â @cards.map{ |card| print card}  
> Â end

to\_s needs to return a string otherwise puts will not use it. Your to\_s  
returns an array (as map returns an array) - an array of nils to be  
precise  
(because print returns nil).  
Also to\_s should not print anything to the screen.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![kaizen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4af34b/32.png) [@kaizen](https://www.ruby-forum.com/u/kaizen)\
**Post date:** [June 14, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/definning-to-s-in-a-class-inexpected-result/140487/3 "2008-06-14T21:14:26Z")

</div>

On Saturday 14 June 2008, AndrÃ©s SuÃ¡rez wrote:

> RANKS = %w{2 3 4 5 6 7 8 9 T J Q K A}  
> def to\_s  
> AndrÃ©s  
> There are two problems with your code. The first is the use of print in  
> the  
> to\_s method. print displays its arguments on screen and returns nil. So,  
> the  
> return value of the @cards.map expression is an array filled with nil  
> elements. The second problem lies in the fact that your to\_s method  
> returns an  
> array.

You can solve both problems using the following definition of Deck#to\_s:

def to\_s  
@cards.join ’ ’  
end

Since the elements of the @cards array are already strings, you don’t  
need to  
convert them. Instead, you have to convert the array itself to a string:  
using  
join will convert each element of the array to a string, then  
concatenates  
them inserting a space between each pair.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![kaizen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4af34b/32.png) [@kaizen](https://www.ruby-forum.com/u/kaizen)\
**Post date:** [June 14, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/definning-to-s-in-a-class-inexpected-result/140487/4 "2008-06-14T21:41:57Z")

</div>

On Sat, Jun 14, 2008 at 9:06 PM, Sebastian H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
You dealt nice with this Sebastian 😉

---

<div class="post-metadata">

**Author:** ![kaizen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4af34b/32.png) [@kaizen](https://www.ruby-forum.com/u/kaizen)\
**Post date:** [June 14, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/definning-to-s-in-a-class-inexpected-result/140487/5 "2008-06-14T22:00:28Z")

</div>

Thanks for the replies!

Now I understand better “print” and “to\_s”.

😃
