Forum: Ruby "puts"ing an 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.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2009-06-09 14:52
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
. . .
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-06-09 15:32
(Received via mailing list)
On Tue, Jun 9, 2009 at 2:52 PM, Peter Bailey<pbailey@bna.com> wrote:
>  file.puts "1 + 2 = #{1+2}"
>  File.open("F:/workflows/graphics/receipts/pngfiles.txt", "w") do |file|
>  file.puts "#{files}"
>  end
> produces:
>  69116.png69251.png69391.pngAZ69080.pngAZ69982.pngcx69362.pngcx69363.png

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.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-06-09 15:48
(Received via mailing list)
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://agileconsultingllc.com
Rob@AgileConsultingLLC.com
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2009-06-09 15:50
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://agileconsultingllc.com
> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-06-09 16:08
(Received via mailing list)
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
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2009-06-09 16:39
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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2009-06-09 16:47
> 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.
This topic is locked and can not be replied to.