[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Definning "to_s" in a class - inexpected result

Andrés Suárez

6/14/2008 7:02:00 PM

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
--
Posted via http://www.ruby-....

6 Answers

Sebastian Hungerecker

6/14/2008 7:07:00 PM

0

Andr=C3=A9s Su=C3=A1rez wrote:
> def to_s
> =C2=A0 =C2=A0@cards.map{ |card| print card}
> =C2=A0 end

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

HTH,
Sebastian
=2D-=20
NP: Depeche Mode - Barrel Of A Gun
Jabber: sepp2k@jabber.org
ICQ: 205544826

Stefano Crocco

6/14/2008 7:13:00 PM

0

On Saturday 14 June 2008, Andr=C3=A9s Su=C3=A1rez wrote:
> 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 =3D %w{s d h c}
> RANKS =3D %w{2 3 4 5 6 7 8 9 T J Q K A}
>
> def initialize
> @cards =3D []
> 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 =3D Deck.new
> puts d0
>
>
> Regards,
> Andr=C3=A9s

There are two problems with your code. The first is the use of print in the=
=20
to_s method. print displays its arguments on screen and returns nil. So, th=
e=20
return value of the @cards.map expression is an array filled with nil=20
elements. The second problem lies in the fact that your to_s method returns=
an=20
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=20
convert them. Instead, you have to convert the array itself to a string: us=
ing=20
join will convert each element of the array to a string, then concatenates=
=20
them inserting a space between each pair.

I hope this helps

Stefano


Robert Dober

6/14/2008 7:41:00 PM

0

On Sat, Jun 14, 2008 at 9:06 PM, Sebastian Hungerecker
<sepp2k@googlemail.com> wrote:
You dealt nice with this Sebastian ;)

Andrés Suárez

6/14/2008 8:00:00 PM

0

Thanks for the replies!

Now I understand better "print" and "to_s".

:D
--
Posted via http://www.ruby-....

Shmaryahu b. Chanoch

3/16/2011 8:25:00 PM

0

On Mar 16, 8:43 am, sheldonlg <sheldo...@thevillages.net> wrote:
> On 3/16/2011 4:06 AM, mos...@mm.huji.ac.il wrote:
>
> > Sabba Hillel<sabbahil...@gmail.com>  writes:
>
> > Excuse the top posting.
>
> > Oy this is bad, it _hurts_ to read it. Thank you so much for your
> > translations. It shows the total hypocrisy of Hamas and all those
> > Arabs who say one thing in English for foriegn consumption and one
> > thing in Arabic for their followers. Is _anybody_ in the US
> > administration listening!!??
>
> Seeing that my daughter, when she was in the Army, went to school in
> Monterrey to learn Russian so that we could gather intelligence on them
> in _their_ language, I am 100% certain that we are doing the same for
> Arabic.
>
> <snip>

DLI is where she went. Most of our linguists attend there.

And, yes, Arabic, Farsi and Pashtun are taught (Chinese is also big).

Was your daughter HUMINT or SIGINT?

Abe Kohen

3/17/2011 12:50:00 AM

0

sheldonlg wrote:
> On 3/16/2011 4:06 AM, moshes@mm.huji.ac.il wrote:
>> Sabba Hillel<sabbahillel@gmail.com> writes:
>>
>> Excuse the top posting.
>>
>> Oy this is bad, it _hurts_ to read it. Thank you so much for your
>> translations. It shows the total hypocrisy of Hamas and all those
>> Arabs who say one thing in English for foriegn consumption and one
>> thing in Arabic for their followers. Is _anybody_ in the US
>> administration listening!!??
>
> Seeing that my daughter, when she was in the Army, went to school in
> Monterrey to learn Russian so that we could gather intelligence on
> them in _their_ language, I am 100% certain that we are doing the
> same for Arabic.

They teach Arabic at Monterey (one 'r'), California, and from what I heard
they do a good job, but "full immersion" in an Arab country is much better.

Abe

P.S. I'm not correcting a typo, as there are other Monterreys ( two 'r's),
e.g., Mexico.