Forum: Ruby returning p array

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.
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-15 18:04
(Received via mailing list)
I like the way in which p prints out the contents of an array. It's easy
for me to read. Is it possible to make a method return p array? Or is
there a better way to do this?

def info
    #create an array
    p array
    return p array
end
1889d44478574daa962c5ea19d3307bb?d=identicon&s=25 Pierre Barbier de Reuille (Guest)
on 2006-03-15 18:04
(Received via mailing list)
rtilley a écrit :
> I like the way in which p prints out the contents of an array. It's
> easy for me to read. Is it possible to make a method return p array?
> Or is there a better way to do this?
>
> def info
>    #create an array
>    p array
>    return p array
> end
"p" just uses the "inspect" method. Thus you can do that :

def info
 # create an array
  p array
  array.inspect
end

Pierre
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-15 18:11
(Received via mailing list)
rtilley wrote:
> I like the way in which p prints out the contents of an array. It's easy
> for me to read. Is it possible to make a method return p array? Or is
> there a better way to do this?
>
> def info
>    #create an array
>    p array
>    return p array
> end

Just to clarify my question... When I create an array like this:

x = Array.new
x.push(1,2,3)

And then write the array to a file, I get a file that reads 123. I would
like a file that looks like this [1, 2, 3] instead.

Thank you,
Brad
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-15 18:11
(Received via mailing list)
"rtilley" <rtilley@vt.edu> wrote in message
news:dv9h29$agf$1@solaris.cc.vt.edu...
>I like the way in which p prints out the contents of an array. It's easy
>for me to read. Is it possible to make a method return p array? Or is
>there a better way to do this?
>
> def info
>    #create an array
>    p array
>    return p array
> end

This won't work because p returns nil.  Do this

def info
  create_array_somehow.inspect
end

Kind regards

    robert
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-15 18:20
(Received via mailing list)
Pierre Barbier de Reuille wrote:
> "p" just uses the "inspect" method. Thus you can do that :
>
> def info
> # create an array
>  p array
>  array.inspect
> end
>
> Pierre

Thank you Pierre and Robert. That is exactly what I wanted to do.

Brad
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-16 10:30
(Received via mailing list)
"rtilley" <rtilley@vt.edu> wrote in message
news:dv9hej$dnv$1@solaris.cc.vt.edu...
>
> Just to clarify my question... When I create an array like this:
>
> x = Array.new
> x.push(1,2,3)

This can be shortened to

x=[1,2,3]

> And then write the array to a file, I get a file that reads 123. I would
> like a file that looks like this [1, 2, 3] instead.

Do x.inspect or x.join ", "

Kind regards

    robert
This topic is locked and can not be replied to.