More Examples

 
   

Ruby Way
By Hal Fulton
Slots : 1.0
Table of Contents
 


Here's another pair of examples for comparison:

 

 #!/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.


   

 

 



The Ruby Way
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2000
Pages: 119
Authors: Hal Fulton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net