5 Files Io


Published on

Published in: Technology, Sports
  • Be the first to comment

  • Be the first to like this

5 Files Io

  1. 1. I/O and Files<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 /> &quot;r&quot; Read-only, starts at beginning of file (default)<br /> &quot;r+&quot; Read-write, starts at beginning of file<br /> &quot;w&quot; Write-only, truncates existing file to zero length or creates a new file for writing<br /> &quot;w+&quot; 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 />&gt;&gt; f = File.new(&quot;abc.rb&quot;)<br />=&gt; #&lt;File:abc.rb&gt;<br />
  5. 5. open file – with a block<br />File.open is synonomous with File.new, except that it can take a block:<br />&gt;&gt; File.open(“abc.rb&quot;, &quot;r+&quot;) do |f| f &lt;&lt; &quot;hello&quot; <br /> end <br />=&gt; #&lt;File:abc.rb (closed)&gt;<br />
  6. 6. read a file<br />return a single line<br />&gt;&gt; f.gets<br />=&gt; &quot;aaa &quot;<br />return the whole file after current position<br />&gt;&gt; f.read<br />=&gt; &quot;aaa bbb ccc&quot;<br />returns file as an array of lines<br />&gt;&gt; f.readlines<br />=&gt; [&quot;aaa &quot;, &quot;bbb &quot;, &quot;ccc&quot;]<br />
  7. 7. position & rewind<br />&gt;&gt; f.rewind<br />=&gt; 0<br />&gt;&gt; f.pos<br />=&gt; 0<br />&gt;&gt; f.gets<br />=&gt; &quot;aaa &quot;<br />&gt;&gt; f.pos<br />=&gt; 4<br />
  8. 8. iterate over lines<br />File objects are enumerable:<br />&gt;&gt; f.each {|line| puts line}<br />aaa<br />bbb<br />ccc<br />
  9. 9. use any enumerable method<br />&gt;&gt; f.map {|line| line.chomp + &quot;... &quot;}<br />=&gt; [&quot;ddd... &quot;, &quot;eee... &quot;, &quot;fff4th letter to 6th... &quot;]<br />&gt;&gt; f.any? {|line| line =~ /ddd / }<br />=&gt; false<br />&gt;&gt; f.rewind<br />=&gt; 0<br />&gt;&gt; f.any? {|line| line =~ /ddd / }<br />=&gt; true<br />
  10. 10. write to a file<br />“r+” argument<br />&gt;&gt; g = File.new(&quot;def.rb&quot;, &quot;r+&quot;)<br />=&gt; #&lt;File:def.rb&gt;<br />&gt;&gt; g.read<br />=&gt; &quot;ddd eee fff&quot;<br />&gt;&gt; g.puts &quot;4th letter to 6th”<br />&gt;&gt; g.rewind<br />&gt;&gt; g.read<br />=&gt; &quot;ddd eee fff4th letter to 6th &quot;<br />
  11. 11. Exercise: Using gem faker to create test data<br />Directions:<br />Create a yaml file with 50 fake names and addresses.<br /><ul><li>Write spec
  12. 12. Make it pass</li></li></ul><li>creating test data<br />sudo gem install faker<br />&gt;&gt; require &apos;faker’<br />&gt;&gt; Faker::Name.name<br />=&gt; &quot;Matteo Kuhic&quot;<br />&gt;&gt; Faker::Address.street_address<br />=&gt; &quot;6413 Timmothy Brook&quot;<br />&gt;&gt; Faker::Address.city<br />=&gt; &quot;Gleichnerhaven&quot;<br />&gt;&gt; Faker::Address.us_state<br />=&gt; &quot;Florida&quot;<br />&gt;&gt; Faker::Address.zip_code<br />=&gt; &quot;40554-7199&quot;<br />
  13. 13. final product:<br />name: Matteo Kuhic<br /> street_address: 6413 Timmothy Brook<br /> city: Gleichnerhaven<br /> state: Florida<br /> zip_code: 40554-7199<br />
  14. 14. Live Coding<br />markdown to textile conversion, with files<br />
  15. 15. directories<br />&gt;&gt; d = Dir.new(&quot;/Users/liahhansen/s/dir&quot;)<br />=&gt; #&lt;Dir:/Users/liahhansen/s/dir&gt;<br />&gt;&gt; d.entries<br />=&gt; [&quot;.&quot;, &quot;..&quot;, &quot;abc.rb&quot;, &quot;anothersubdir&quot;, &quot;def.rb&quot;, &quot;ghi.rb&quot;, &quot;subdirectory&quot;]<br />
  16. 16. count files in directory<br />&gt;&gt; i=0<br />=&gt; 0<br />&gt;&gt; d.each {|x| i+=1; puts i}<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br />
  17. 17. iterate over files in directory<br />&gt;&gt; ruby_files = Dir[&quot;*.rb&quot;]<br />=&gt; [&quot;abc.rb&quot;, &quot;def.rb&quot;, &quot;ghi.rb&quot;]<br />&gt;&gt; ruby_files.map do |file| <br /> &quot;rb_&quot; + file<br /> end<br />=&gt; [&quot;rb_abc.rb&quot;, &quot;rb_def.rb&quot;, &quot;rb_ghi.rb&quot;]<br />
  18. 18. Homework<br />Chapters:<br />12<br />Exercises:<br />make index for directory<br />