[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String Problems?

Carter Davis

11/8/2008 8:11:00 PM

There is probably an easy answer to this, but I need help. Recently I've
started making a text-based adventure game in Ruby, and so far it's
going very well. One problem I'm having, though, is that whenever I use
puts to output a string, the string is in quotes. Is there any way in
which I can disable this?

Also, I can't seem to get string literals to work. I'll type in, for
example,

p "You see a glass cup, and there is a door NORTH.\n There is also a
long hallway SOUTH."

And it will output the exact same - when I run the code, it says:
"You see a glass cup, and there is a door NORTH.\n There is also a long
hallway SOUTH."

I have no idea how to fix said problems...can anyone help me?

Thanks,
Jearb
--
Posted via http://www.ruby-....

3 Answers

Stefano Crocco

11/8/2008 8:17:00 PM

0

Alle Saturday 08 November 2008, Carter Davis ha scritto:
> whenever I use
> puts to output a string, the string is in quotes.
...
> I'll type in, for example,
>
> p "You see a glass cup, and there is a door NORTH.\n There is also a
> long hallway SOUTH."
>

You aren't using puts, you're using p, which is a different thing. puts calls
to_s on its arguments, while p calls inspect. In the case of strings, to_s
gives a human-readable text, while inspect returns a version with all special
characters escaped. For example:

s = "You see a glass cup, and there is a door NORTH.\n There is also a
long hallway SOUTH."
s.to_s

gives

You see a glass cup, and there is a door NORTH.
There is also a long hallway SOUTH.

while

s.inspect

gives

"You see a glass cup, and there is a door NORTH.\n There is also along hallway
SOUTH."

which is want you get. Replace p with puts and it should work as you want.

I hope this helps

Stefano

Sebastian Hungerecker

11/8/2008 8:24:00 PM

0

Carter Davis wrote:
> One problem I'm having, though, is that whenever I use
> puts to output a string, the string is in quotes. Is there any way in
> which I can disable this?
> ...
> p "You see a glass cup, and there is a door NORTH.\n There is also a
> long hallway SOUTH."

It seems to me that the problem you're having is that whenever you're using
*p* you get the string in quotes (and with all control characters displayed
in escaped form), which is what it's supposed to do.
If you do have the same problem with puts, please show the code using puts
that exhibits this behaviour.

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Carter Davis

11/8/2008 8:32:00 PM

0

Thank you so much! It works now. :D

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