Forum: Ruby How does one get a String representation of an Array (not to

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.
C577f603b752a8a4f5cb34a423316a71?d=identicon&s=25 unknown (Guest)
on 2006-02-22 18:50
(Received via mailing list)
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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-02-22 18:56
(Received via mailing list)
On Thu, 23 Feb 2006 junk5@microserf.org.uk wrote:

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

-a
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-02-22 18:56
(Received via mailing list)
On Thu, 2006-02-23 at 02:48 +0900, junk5@microserf.org.uk wrote:
> => [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]
7df03228fdadcf3bc7b10439e4e7e1fd?d=identicon&s=25 1337p337 (Guest)
on 2006-02-22 18:56
(Received via mailing list)
Hi,

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

Pete
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-22 18:59
(Received via mailing list)
junk5@microserf.org.uk wrote:
> => [1, 2, [3, 4], 5]
> C
puts a.inspect

Or just

p a

Kind regards

    robert
C577f603b752a8a4f5cb34a423316a71?d=identicon&s=25 unknown (Guest)
on 2006-02-22 19:05
(Received via mailing list)
Thanks all!
This topic is locked and can not be replied to.