#!/usr/bin/perl @list = `ls -l *.htm?`; open(FH,">list.txt") or die "Error opening file: $!\n"; foreach (@list){ print FH; } close FH; #!/usr/local/bin/ruby list = `ls -l *.htm?` File.open("list.txt","w") { |fh| list.each { |line| fh.print line } }
The results of the shell command are put into a single string in Ruby, including the newlines. Later, the each iterator retrieves each line from that string.
There is no close statement used here because the File.open method automatically closes it upon completion of the associated block. It also automatically raises an exception in case of an error.