How does one get a String representation of an Array (not to_s)?

Discussion in 'Ruby' started by junk5@microserf.org.uk, Feb 22, 2006.

  1. Guest

    Hi all

    (I know about to_s --- that isn't what this post is about!)

    Say I have an array that I create with:

    a = [1, 2, [3, 4], 5]

    At the irb prompt, Ruby replies:

    => [1, 2, [3, 4], 5]

    So how do I get a String containing "[1, 2, [3, 4], 5]"? Doing a.to_s
    gives me "12345", which is not what I want.

    I need this for debugging purposes and would rather not have to write
    my own array printer. It must be easy, but I can't see which function I
    should use.

    Thanks in advace,

    C
     
    , Feb 22, 2006
    #1
    1. Advertising

  2. Guest

    On Thu, 23 Feb 2006 wrote:

    > Hi all
    >
    > (I know about to_s --- that isn't what this post is about!)
    >
    > Say I have an array that I create with:
    >
    > a = [1, 2, [3, 4], 5]
    >
    > At the irb prompt, Ruby replies:
    >
    > => [1, 2, [3, 4], 5]
    >
    > So how do I get a String containing "[1, 2, [3, 4], 5]"? Doing a.to_s
    > gives me "12345", which is not what I want.
    >
    > I need this for debugging purposes and would rather not have to write
    > my own array printer. It must be easy, but I can't see which function I
    > should use.
    >
    > Thanks in advace,
    >
    > C


    inspect

    -a

    --
    judge your success by what you had to give up in order to get it.
    - h.h. the 14th dali lama
     
    , Feb 22, 2006
    #2
    1. Advertising

  3. Ross Bamford Guest

    Re: How does one get a String representation of an Array (notto_s)?

    On Thu, 2006-02-23 at 02:48 +0900, wrote:
    > Hi all
    >
    > (I know about to_s --- that isn't what this post is about!)
    >
    > Say I have an array that I create with:
    >
    > a = [1, 2, [3, 4], 5]
    >
    > At the irb prompt, Ruby replies:
    >
    > => [1, 2, [3, 4], 5]
    >
    > So how do I get a String containing "[1, 2, [3, 4], 5]"? Doing a.to_s
    > gives me "12345", which is not what I want.


    s = [1,2,[3,4],5].inspect
    # => "[1, 2, [3, 4], 5]"

    puts s
    [1, 2, [3, 4], 5]

    --
    Ross Bamford -
     
    Ross Bamford, Feb 22, 2006
    #3
  4. wrote:
    > Hi all
    >
    > (I know about to_s --- that isn't what this post is about!)
    >
    > Say I have an array that I create with:
    >
    > a = [1, 2, [3, 4], 5]
    >
    > At the irb prompt, Ruby replies:
    >
    > => [1, 2, [3, 4], 5]
    >
    > So how do I get a String containing "[1, 2, [3, 4], 5]"? Doing a.to_s
    > gives me "12345", which is not what I want.
    >
    > I need this for debugging purposes and would rather not have to write
    > my own array printer. It must be easy, but I can't see which function
    > I should use.
    >
    > Thanks in advace,
    >
    > C


    puts a.inspect

    Or just

    p a

    Kind regards

    robert
     
    Robert Klemme, Feb 22, 2006
    #4
  5. 1337p337 Guest

    Hi,

    a.inspect is probably what you're looking for. 'p a' will print it
    directly to stdout.

    Pete

    On 22/02/06, <> wrote:
    > Hi all
    >
    > (I know about to_s --- that isn't what this post is about!)
    >
    > Say I have an array that I create with:
    >
    > a =3D [1, 2, [3, 4], 5]
    >
    > At the irb prompt, Ruby replies:
    >
    > =3D> [1, 2, [3, 4], 5]
    >
    > So how do I get a String containing "[1, 2, [3, 4], 5]"? Doing a.to_s
    > gives me "12345", which is not what I want.
    >
    > I need this for debugging purposes and would rather not have to write
    > my own array printer. It must be easy, but I can't see which function I
    > should use.
    >
    > Thanks in advace,
    >
    > C
    >
    >
    >
     
    1337p337, Feb 22, 2006
    #5
  6. Guest

    Thanks all!
     
    , Feb 22, 2006
    #6
    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. Hessam
    Replies:
    0
    Views:
    2,188
    Hessam
    Aug 8, 2003
  2. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,808
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  3. Hessam
    Replies:
    1
    Views:
    239
    Teemu Keiski
    Aug 16, 2003
  4. Hessam
    Replies:
    0
    Views:
    274
    Hessam
    Aug 8, 2003
  5. Iñaki Baz Castillo
    Replies:
    4
    Views:
    107
    Iñaki Baz Castillo
    Jul 1, 2008
Loading...

Share This Page