String Problems?

C

Carter Davis

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
 
S

Stefano Crocco

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
 
S

Sebastian Hungerecker

Carter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top