String Problems?

Discussion in 'Ruby' started by Carter Davis, Nov 8, 2008.

  1. Carter Davis

    Carter Davis Guest

    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-forum.com/.
     
    Carter Davis, Nov 8, 2008
    #1
    1. Advertising

  2. 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
     
    Stefano Crocco, Nov 8, 2008
    #2
    1. Advertising

  3. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Nov 8, 2008
    #3
  4. Carter Davis

    Carter Davis Guest

    Carter Davis, Nov 8, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    741
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,617
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    510
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,933
    John C. Bollinger
    Nov 19, 2004
  5. Susan Baker
    Replies:
    2
    Views:
    816
    kelvSYC
    Jun 26, 2005
Loading...

Share This Page