• Like
5 Files Io
Upcoming SlideShare
Loading in...5
×
Uploaded on

 

More in: Technology , Sports
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
278
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
5
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • The File class is a subclass of the IO class
  • rewind
  • make a spec, make it pass

Transcript

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