Forum: Ruby String Problems?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Carter D. (Guest)
on 2008-11-08 22:13
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
Stefano C. (Guest)
on 2008-11-08 22:19
(Received via mailing list)
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 H. (Guest)
on 2008-11-08 22:26
(Received via mailing list)
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
Carter D. (Guest)
on 2008-11-08 22:33
Thank you so much! It works now. :D
This topic is locked and can not be replied to.