Ruby File I/O

3,708 views

Published on

Reading and Writing Files in Ruby

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,708
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
31
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • The File class is a subclass of the IO class
  • rewind
  • Ruby File I/O

    1. 1. I/O and Files<br />by Liah Hansen and Sarah Allen<br />
    2. 2. IO: Input/Output<br />IO class is the basis for all input and output in Ruby<br />IO objects represent readable and/or writable connections to disk files, keyboards & screens <br />File is a subclass of IO and allows for reading and writing files in ruby<br />
    3. 3. common modes for opening I/O ports<br /> "r" Read-only, starts at beginning of file (default)<br /> "r+" Read-write, starts at beginning of file<br /> "w" Write-only, truncates existing file to zero length or creates a new file for writing<br /> "w+" Read-write, truncates existing file to zero length or creates a new file for reading and writing<br />
    4. 4. open a file<br />Creating the file object opens the file: <br />>> f = File.new("abc.rb")<br />=> #<File:abc.rb><br />
    5. 5. open file – with a block<br />File.open is synonomous with File.new, except that it can take a block:<br />>> File.open(“abc.rb", "r+") do |f| f << "hello" <br /> end <br />=> #<File:abc.rb (closed)><br />
    6. 6. read a file<br />return a single line<br />>> f.gets<br />=> "aaa "<br />return the whole file after current position<br />>> f.read<br />=> "aaa bbb ccc"<br />returns file as an array of lines<br />>> f.readlines<br />=> ["aaa ", "bbb ", "ccc"]<br />
    7. 7. position & rewind<br />>> f.rewind<br />=> 0<br />>> f.pos<br />=> 0<br />>> f.gets<br />=> "aaa "<br />>> f.pos<br />=> 4<br />
    8. 8. iterate over lines<br />File objects are enumerable:<br />>> f.each {|line| puts line}<br />aaa<br />bbb<br />ccc<br />
    9. 9. use any enumerable method<br />>> f.map {|line| line.chomp + "... "}<br />=> [”aaa... ", ”bbb... ", ”ccc... "]<br />>> f.any? {|line| line =~ /aaa / }<br />=> false<br />>> f.rewind<br />=> 0<br />>> f.any? {|line| line =~ /aaa / }<br />=> true<br />
    10. 10. write to a file<br />“w+” argument<br />>>my_file= File.new("data.txt", "w+")<br />=> #<File:def.rb><br />>> my_file.write("something")<br />=> 9<br />>> my_file.rewind<br />=> 0<br />>> my_file.read<br />=> "something"<br />
    11. 11. directories<br />>> d = Dir.new("/Users/liahhansen/s/dir")<br />=> #<Dir:/Users/liahhansen/s/dir><br />>> d.entries<br />=> [".", "..", "abc.rb", "anothersubdir", "def.rb", "ghi.rb", "subdirectory"]<br />
    12. 12. count files in directory<br />>> i=0<br />=> 0<br />>> d.each {|x| i+=1; puts i}<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br />
    13. 13. iterate over files in directory<br />>> ruby_files = Dir["*.rb"]<br />=> ["abc.rb", "def.rb", "ghi.rb"]<br />>> ruby_files.map do |file| <br /> "rb_" + file<br /> end<br />=> ["rb_abc.rb", "rb_def.rb", "rb_ghi.rb"]<br />

    ×