SlideShare a Scribd company logo
1 of 13
I/O and Files by Liah Hansen and Sarah Allen
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
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 file Creating the file object opens the file:  >> f = File.new("abc.rb") => #<File:abc.rb>
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)>
read a file return a single line >> f.gets => "aaa" return the whole file after current position >> f.read => "aaabbbccc" returns file as an array of lines >> f.readlines => ["aaa", "bbb", "ccc"]
position & rewind >> f.rewind => 0 >> f.pos => 0 >> f.gets => "aaa" >> f.pos => 4
iterate over lines File objects are enumerable: >> f.each {|line| puts line} aaa bbb ccc
use any enumerable method >> f.map {|line| line.chomp +  "... "} => [”aaa... ", ”bbb... ", ”ccc... "] >> f.any? {|line| line =~ /aaa/ } => false >> f.rewind => 0 >> f.any? {|line| line =~ /aaa/ } => true
write to a file “w+” argument >>my_file= File.new("data.txt", "w+") => #<File:def.rb> >> my_file.write("something") => 9 >> my_file.rewind => 0 >> my_file.read => "something"
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} 1 2 3 4 5 6 7
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"]

More Related Content

What's hot (20)

Files in c++
Files in c++Files in c++
Files in c++
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Files in c++
Files in c++Files in c++
Files in c++
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
Fluentd Unified Logging Layer At Fossasia
Fluentd Unified Logging Layer At FossasiaFluentd Unified Logging Layer At Fossasia
Fluentd Unified Logging Layer At Fossasia
 
Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
file handling c++
file handling c++file handling c++
file handling c++
 
The Functional Web
The Functional WebThe Functional Web
The Functional Web
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
 
File Pointers
File PointersFile Pointers
File Pointers
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
File handling
File handlingFile handling
File handling
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 
file handling, dynamic memory allocation
file handling, dynamic memory allocationfile handling, dynamic memory allocation
file handling, dynamic memory allocation
 
Code tacoma command_line
Code tacoma command_lineCode tacoma command_line
Code tacoma command_line
 
Comp102 lec 11
Comp102   lec 11Comp102   lec 11
Comp102 lec 11
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 

Viewers also liked (8)

16 ruby hashes
16 ruby hashes16 ruby hashes
16 ruby hashes
 
19 ruby iterators
19 ruby iterators19 ruby iterators
19 ruby iterators
 
15 ruby arrays
15 ruby arrays15 ruby arrays
15 ruby arrays
 
17 ruby date time
17 ruby date time17 ruby date time
17 ruby date time
 
14 ruby strings
14 ruby strings14 ruby strings
14 ruby strings
 
20 ruby input output
20 ruby input output20 ruby input output
20 ruby input output
 
18 ruby ranges
18 ruby ranges18 ruby ranges
18 ruby ranges
 
8 Key Life and Leadership Lessons
8 Key Life and Leadership Lessons8 Key Life and Leadership Lessons
8 Key Life and Leadership Lessons
 

Similar to Ruby File I/O (20)

Files IO
Files IOFiles IO
Files IO
 
C++ io manipulation
C++ io manipulationC++ io manipulation
C++ io manipulation
 
Python file handlings
Python file handlingsPython file handlings
Python file handlings
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
Rush, a shell that will yield to you
Rush, a shell that will yield to youRush, a shell that will yield to you
Rush, a shell that will yield to you
 
Ant
Ant Ant
Ant
 
Apache ant
Apache antApache ant
Apache ant
 
Apache Ant
Apache AntApache Ant
Apache Ant
 
Ant User Guide
Ant User GuideAnt User Guide
Ant User Guide
 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Park
 
Files
FilesFiles
Files
 
Files
FilesFiles
Files
 
php file uploading
php file uploadingphp file uploading
php file uploading
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
 
File Handling as 08032021 (1).ppt
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
data file handling
data file handlingdata file handling
data file handling
 
Introduction To Ant
Introduction To AntIntroduction To Ant
Introduction To Ant
 
FILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptxFILE INPUT OUTPUT.pptx
FILE INPUT OUTPUT.pptx
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 

More from Sarah Allen

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesSarah Allen
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)Sarah Allen
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical SkillSarah Allen
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government ServicesSarah Allen
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency WinsSarah Allen
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of ComputersSarah Allen
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software FunSarah Allen
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of TransparencySarah Allen
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for FunSarah Allen
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceSarah Allen
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead PeopleSarah Allen
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby Sarah Allen
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Sarah Allen
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?Sarah Allen
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersSarah Allen
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentSarah Allen
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeSarah Allen
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future ThoughtsSarah Allen
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web VideoSarah Allen
 

More from Sarah Allen (20)

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequences
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical Skill
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government Services
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency Wins
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of Computers
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software Fun
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of Transparency
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for Fun
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World Conference
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead People
 
Let's pretend
Let's pretendLet's pretend
Let's pretend
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from Toastmasters
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product Development
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription Landscape
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future Thoughts
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web Video
 

Ruby File I/O

  • 1. I/O and Files by Liah Hansen and Sarah Allen
  • 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 => "aaabbbccc" 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 + "... "} => [”aaa... ", ”bbb... ", ”ccc... "] >> f.any? {|line| line =~ /aaa/ } => false >> f.rewind => 0 >> f.any? {|line| line =~ /aaa/ } => true
  • 10. write to a file “w+” argument >>my_file= File.new("data.txt", "w+") => #<File:def.rb> >> my_file.write("something") => 9 >> my_file.rewind => 0 >> my_file.read => "something"
  • 11. directories >> d = Dir.new("/Users/liahhansen/s/dir") => #<Dir:/Users/liahhansen/s/dir> >> d.entries => [".", "..", "abc.rb", "anothersubdir", "def.rb", "ghi.rb", "subdirectory"]
  • 12. count files in directory >> i=0 => 0 >> d.each {|x| i+=1; puts i} 1 2 3 4 5 6 7
  • 13. 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

  1. The File class is a subclass of the IO class
  2. rewind