SlideShare a Scribd company logo
I/O and Files
IO: Input/OutputIO class is the basis for all input and output in RubyIO 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
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
open a fileCreating the file object opens the file: >> f = File.new("abc.rb")=> #<File:abc.rb>
open file – with a blockFile.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)>
read a filereturn a single line>> f.gets=> "aaa\n"return the whole file after current position>> f.read=> "aaa\nbbb\nccc"returns file as an array of lines>> f.readlines=> ["aaa\n", "bbb\n", "ccc"]
position & rewind>> f.rewind=> 0>> f.pos=> 0>> f.gets=> "aaa\n">> f.pos=> 4
iterate over linesFile objects are enumerable:>> f.each {|line| puts line}aaabbbccc
use any enumerable method>> f.map {|line| line.chomp +  "... "}=> ["ddd... ", "eee... ", "fff4th letter to 6th... "]>> f.any? {|line| line =~ /ddd\n/ }=> false>> f.rewind=> 0>> f.any? {|line| line =~ /ddd\n/ }=> true
write to a file“r+” argument>> g = File.new("def.rb", "r+")=> #<File:def.rb>>> g.read=> "ddd\neee\nfff">> g.puts "4th letter to 6th”>> g.rewind>> g.read=> "ddd\neee\nfff4th letter to 6th\n"
Exercise: Using gem faker to create test dataDirections:Create a yaml file with 50 fake names and addresses.Write spec
Make it passcreating test datasudo 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"
final product:name: Matteo Kuhic	street_address: 6413 Timmothy Brook	city: Gleichnerhaven	state: Florida	zip_code: 40554-7199
Live Codingmarkdown to textile conversion, with files
directories>> d = Dir.new("/Users/liahhansen/s/dir")=> #<Dir:/Users/liahhansen/s/dir>>> d.entries=> [".", "..", "abc.rb", "anothersubdir", "def.rb", "ghi.rb", "subdirectory"]
count files in directory>> i=0=> 0>> d.each {|x| i+=1; puts i}1234567
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"]
HomeworkChapters:12Exercises:make index for directory

More Related Content

PPTX
Ruby File I/O
PDF
Android nálevna (Czech / Android for beginners)
PPTX
Java Web Start
PPTX
PHP presentation
PDF
Google I/O 2013 - Android Graphics Performance
PDF
3 idle mode
PDF
WordCamp 2010: Twenty Ten barrierefrei?
KEY
Files IO
Ruby File I/O
Android nálevna (Czech / Android for beginners)
Java Web Start
PHP presentation
Google I/O 2013 - Android Graphics Performance
3 idle mode
WordCamp 2010: Twenty Ten barrierefrei?
Files IO

Similar to 5 Files Io (20)

PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
20 ruby input output
PPTX
9 Inputs & Outputs
PDF
Ruby 程式語言簡介
PDF
Ruby Topic Maps Tutorial (2007-10-10)
PPT
name name2 n2.ppt
PPT
name name2 n
PPT
name name2 n2
PPT
name name2 n
PPT
ppt18
PPT
ppt9
PPT
ppt7
PPT
ppt30
PPT
ppt21
PPT
ppt2
PPT
ppt17
PPT
Ruby for Perl Programmers
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
20 ruby input output
9 Inputs & Outputs
Ruby 程式語言簡介
Ruby Topic Maps Tutorial (2007-10-10)
name name2 n2.ppt
name name2 n
name name2 n2
name name2 n
ppt18
ppt9
ppt7
ppt30
ppt21
ppt2
ppt17
Ruby for Perl Programmers
Ad

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
KodekX | Application Modernization Development
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Machine learning based COVID-19 study performance prediction
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The AUB Centre for AI in Media Proposal.docx
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Understanding_Digital_Forensics_Presentation.pptx
Big Data Technologies - Introduction.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KodekX | Application Modernization Development
NewMind AI Weekly Chronicles - August'25 Week I
Machine learning based COVID-19 study performance prediction
Dropbox Q2 2025 Financial Results & Investor Presentation
Network Security Unit 5.pdf for BCA BBA.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
Ad

5 Files Io

  • 2. IO: Input/OutputIO class is the basis for all input and output in RubyIO 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 fileCreating the file object opens the file: >> f = File.new("abc.rb")=> #<File:abc.rb>
  • 5. open file – with a blockFile.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 filereturn a single line>> f.gets=> "aaa\n"return the whole file after current position>> f.read=> "aaa\nbbb\nccc"returns file as an array of lines>> f.readlines=> ["aaa\n", "bbb\n", "ccc"]
  • 7. position & rewind>> f.rewind=> 0>> f.pos=> 0>> f.gets=> "aaa\n">> f.pos=> 4
  • 8. iterate over linesFile objects are enumerable:>> f.each {|line| puts line}aaabbbccc
  • 9. use any enumerable method>> f.map {|line| line.chomp + "... "}=> ["ddd... ", "eee... ", "fff4th letter to 6th... "]>> f.any? {|line| line =~ /ddd\n/ }=> false>> f.rewind=> 0>> f.any? {|line| line =~ /ddd\n/ }=> true
  • 10. write to a file“r+” argument>> g = File.new("def.rb", "r+")=> #<File:def.rb>>> g.read=> "ddd\neee\nfff">> g.puts "4th letter to 6th”>> g.rewind>> g.read=> "ddd\neee\nfff4th letter to 6th\n"
  • 11. Exercise: Using gem faker to create test dataDirections:Create a yaml file with 50 fake names and addresses.Write spec
  • 12. Make it passcreating test datasudo 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 Codingmarkdown 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}1234567
  • 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"]

Editor's Notes

  • #5: The File class is a subclass of the IO class
  • #7: rewind
  • #13: make a spec, make it pass