[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"puts"ing an array

Peter Bailey

6/9/2009 12:53:00 PM

Hi,
In the "pickaxe" book I see how to use a "puts" to populate a file with
data. I try the same thing, and, it works, but, it's listing the data as
an array. How can I get separate lines for each item in the array?
Thanks,
Peter

In the book:
File.open("output.txt", "w") do |file|
file.puts "Hello"
file.puts "1 + 2 = #{1+2}"
end
# Now read the file in and print its contents to STDOUT
puts File.read("output.txt")
produces:
Hello
1 + 2 = 3

My script:
Dir.chdir("L:/png/69000")
files = Dir.glob("*.png")
File.open("F:/workflows/graphics/receipts/pngfiles.txt", "w") do |file|
file.puts "#{files}"
end
produces:
69116.png69251.png69391.pngAZ69080.pngAZ69982.pngcx69362.pngcx69363.png
. .
--
Posted via http://www.ruby-....

6 Answers

Jesús Gabriel y Galán

6/9/2009 1:31:00 PM

0

On Tue, Jun 9, 2009 at 2:52 PM, Peter Bailey<pbailey@bna.com> wrote:
> Hi,
> In the "pickaxe" book I see how to use a "puts" to populate a file with
> data. I try the same thing, and, it works, but, it's listing the data as
> an array. How can I get separate lines for each item in the array?
> Thanks,
> Peter
>
> In the book:
> =A0File.open("output.txt", "w") do |file|
> =A0file.puts "Hello"
> =A0file.puts "1 + 2 =3D #{1+2}"
> =A0end
> =A0# Now read the file in and print its contents to STDOUT
> =A0puts File.read("output.txt")
> produces:
> =A0Hello
> =A01 + 2 =3D 3
>
> My script:
> =A0Dir.chdir("L:/png/69000")
> =A0files =3D Dir.glob("*.png")
> =A0File.open("F:/workflows/graphics/receipts/pngfiles.txt", "w") do |file=
|
> =A0file.puts "#{files}"
> =A0end
> produces:
> =A069116.png69251.png69391.pngAZ69080.pngAZ69982.pngcx69362.pngcx69363.pn=
g

Dir.glob returns an array, so two possibilities are (untested):

File.open("F:/workflows/graphics/receipts/pngfiles.txt", "w") do |file|
file.puts files.join("\n")
end

File.open("F:/workflows/graphics/receipts/pngfiles.txt", "w") do |file|
files.each {|f| file.puts f }
end

Hope this helps,

Jesus.

Rob Biedenharn

6/9/2009 1:46:00 PM

0

On Jun 9, 2009, at 8:52 AM, Peter Bailey wrote:

> Hi,
> In the "pickaxe" book I see how to use a "puts" to populate a file
> with
> data. I try the same thing, and, it works, but, it's listing the
> data as
> an array. How can I get separate lines for each item in the array?
> Thanks,
> Peter
>
> In the book:
> File.open("output.txt", "w") do |file|
> file.puts "Hello"
> file.puts "1 + 2 = #{1+2}"
> end
> # Now read the file in and print its contents to STDOUT
> puts File.read("output.txt")
> produces:
> Hello
> 1 + 2 = 3
>
> My script:
> Dir.chdir("L:/png/69000")
> files = Dir.glob("*.png")
Dir.glob returns an Array
>
> File.open("F:/workflows/graphics/receipts/pngfiles.txt", "w") do |
> file|
> file.puts "#{files}"
> end
The default Array#to_s is like Array#join(''). You probably want to
do either:
file.puts files.join("\n")
or iterate on the files:
files.each do |pngfilename|
file.puts pngfilename
end
>
> produces:
> 69116
> .png69251.png69391.pngAZ69080.pngAZ69982.pngcx69362.pngcx69363.png
> . . .


-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com


Peter Bailey

6/9/2009 1:50:00 PM

0

Rob Biedenharn wrote:
> On Jun 9, 2009, at 8:52 AM, Peter Bailey wrote:
>
>> File.open("output.txt", "w") do |file|
>> Dir.chdir("L:/png/69000")
>> files = Dir.glob("*.png")
> Dir.glob returns an Array
>>
>> File.open("F:/workflows/graphics/receipts/pngfiles.txt", "w") do |
>> file|
>> file.puts "#{files}"
>> end
> The default Array#to_s is like Array#join(''). You probably want to
> do either:
> file.puts files.join("\n")
> or iterate on the files:
> files.each do |pngfilename|
> file.puts pngfilename
> end
>>
>> produces:
>> 69116
>> .png69251.png69391.pngAZ69080.pngAZ69982.pngcx69362.pngcx69363.png
>> . . .
>
>
> -Rob
>
> Rob Biedenharn http://agileconsult...
> Rob@AgileConsultingLLC.com

Thank you very much, gentlemen. Yes, I did actually figure it out by
simply making two loops, one for the file and one for the png files
inside it.
Cheers,
Peter
--
Posted via http://www.ruby-....

Robert Klemme

6/9/2009 2:08:00 PM

0

2009/6/9 Rob Biedenharn <Rob@agileconsultingllc.com>:
> On Jun 9, 2009, at 8:52 AM, Peter Bailey wrote:
>
>> Hi,
>> In the "pickaxe" book I see how to use a "puts" to populate a file with
>> data. I try the same thing, and, it works, but, it's listing the data as
>> an array. How can I get separate lines for each item in the array?
>> Thanks,
>> Peter
>>
>> In the book:
>> File.open("output.txt", "w") do |file|
>> file.puts "Hello"
>> file.puts "1 + 2 =3D #{1+2}"
>> end
>> # Now read the file in and print its contents to STDOUT
>> puts File.read("output.txt")
>> produces:
>> Hello
>> 1 + 2 =3D 3
>>
>> My script:
>> Dir.chdir("L:/png/69000")
>> files =3D Dir.glob("*.png")
>
> Dir.glob returns an Array
>>
>> File.open("F:/workflows/graphics/receipts/pngfiles.txt", "w") do |file|
>> =A0file.puts "#{files}"
>> end
>
> The default Array#to_s is like Array#join(''). =A0You probably want to do
> either:
> =A0file.puts files.join("\n")
> or iterate on the files:
> =A0files.each do |pngfilename|
> =A0 =A0file.puts pngfilename
> =A0end

It's even simpler:

file.puts files

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Peter Bailey

6/9/2009 2:40:00 PM

0

Robert Klemme wrote:
> 2009/6/9 Rob Biedenharn <Rob@agileconsultingllc.com>:
>>> File.open("output.txt", "w") do |file|
>>> Dir.chdir("L:/png/69000")
>> �file.puts files.join("\n")
>> or iterate on the files:
>> �files.each do |pngfilename|
>> � �file.puts pngfilename
>> �end
>
> It's even simpler:
>
> file.puts files
>
> Kind regards
>
> robert

Wow. Sometimes Ruby is too beautifully simple.
Thanks, Robert.
Peter
--
Posted via http://www.ruby-....

Marc Heiler

6/9/2009 2:48:00 PM

0

> Sometimes Ruby is too beautifully simple.

It inspired my general rule that if something is not looking beautiful -
or if it looks downright ugly - there must be a better way.
--
Posted via http://www.ruby-....