SlideShare a Scribd company logo
I/O and Filesby Liah Hansen and Sarah Allen
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 +  "... "}=> [”aaa... ", ”bbb... ", ”ccc... "]>> f.any? {|line| line =~ /aaa\n/ }=> false>> f.rewind=> 0>> f.any? {|line| line =~ /aaa\n/ }=> 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}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"]

More Related Content

PPTX
5 Files Io
PPT
C++ files and streams
PPT
File in cpp 2016
PDF
Filehadnling
PPTX
Python data file handling
PPTX
File handling in c++
PPT
Filehandlinging cp2
5 Files Io
C++ files and streams
File in cpp 2016
Filehadnling
Python data file handling
File handling in c++
Filehandlinging cp2

What's hot (20)

PPSX
Files in c++
PPTX
Pf cs102 programming-8 [file handling] (1)
PPTX
Files in c++
PPT
working file handling in cpp overview
PDF
Fluentd Unified Logging Layer At Fossasia
PDF
Filepointers1 1215104829397318-9
PDF
file handling c++
PPTX
The Functional Web
PPT
17 files and streams
PDF
C++ Files and Streams
PPTX
COM1407: File Processing
PPT
File handling
PPT
Cpp file-handling
PPT
file handling, dynamic memory allocation
PPTX
Code tacoma command_line
PPT
Comp102 lec 11
PPT
File Handling In C++(OOPs))
Files in c++
Pf cs102 programming-8 [file handling] (1)
Files in c++
working file handling in cpp overview
Fluentd Unified Logging Layer At Fossasia
Filepointers1 1215104829397318-9
file handling c++
The Functional Web
17 files and streams
C++ Files and Streams
COM1407: File Processing
File handling
Cpp file-handling
file handling, dynamic memory allocation
Code tacoma command_line
Comp102 lec 11
File Handling In C++(OOPs))
Ad

Viewers also liked (8)

PDF
16 ruby hashes
PDF
19 ruby iterators
PDF
15 ruby arrays
PDF
17 ruby date time
PDF
14 ruby strings
PDF
20 ruby input output
PDF
18 ruby ranges
PPTX
8 Key Life and Leadership Lessons
16 ruby hashes
19 ruby iterators
15 ruby arrays
17 ruby date time
14 ruby strings
20 ruby input output
18 ruby ranges
8 Key Life and Leadership Lessons
Ad

Similar to Ruby File I/O (20)

KEY
Files IO
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
Ruby presentasjon på NTNU 22 april 2009
PPTX
9 Inputs & Outputs
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
PPT
name name2 n
PPT
test ppt
Files IO
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
9 Inputs & Outputs
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
name name2 n
test ppt

More from Sarah Allen (20)

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

Ruby File I/O

  • 1. I/O and Filesby Liah Hansen and Sarah Allen
  • 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 + "... "}=> [”aaa... ", ”bbb... ", ”ccc... "]>> f.any? {|line| line =~ /aaa\n/ }=> false>> f.rewind=> 0>> f.any? {|line| line =~ /aaa\n/ }=> 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}1234567
  • 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

  • #5: The File class is a subclass of the IO class
  • #7: rewind