returning p array

R

rtilley

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
 
R

Robert Klemme

rtilley said:
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
 
P

Pierre Barbier de Reuille

rtilley a =E9crit :
I like the way in which p prints out the contents of an array. It's=20
easy for me to read. Is it possible to make a method return p array?=20
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
 
R

rtilley

rtilley said:
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
 
R

rtilley

Pierre said:
"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
 
R

Robert Klemme

rtilley said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top