SlideShare a Scribd company logo
I/O and Files
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 +  "... "} => ["ddd... ", "eee... ", "fff4th letter to 6th... "] >> f.any? {|line| line =~ /ddd/ } => false >> f.rewind => 0 >> f.any? {|line| line =~ /ddd/ } => true
write to a file “r+” argument >> g = File.new("def.rb", "r+") => #<File:def.rb> >> g.read => "dddeeefff" >> g.puts "4th letter to 6th” >> g.rewind >> g.read => "dddeeefff4th letter to 6th"
Exercise: Using gem faker to create test data Directions: Create a yaml file with 50 fake names and addresses. ,[object Object]
Make it pass,[object Object]
final product: name: Matteo Kuhic 	street_address: 6413 Timmothy Brook 	city: Gleichnerhaven 	state: Florida 	zip_code: 40554-7199
Live Coding markdown 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} 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"]
Homework Chapters: 12 Exercises: make index for directory

More Related Content

Similar to 5 Files Io

PHP Presentation
PHP PresentationPHP Presentation
PHP Presentation
Nikhil Jain
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
mussawir20
 
OSS BarCamp Mumbai - JSON Presentation and Demo
OSS BarCamp Mumbai - JSON Presentation and DemoOSS BarCamp Mumbai - JSON Presentation and Demo
OSS BarCamp Mumbai - JSON Presentation and Demo
Ketan Khairnar
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line tools
Eric Wilson
 
XML processing with perl
XML processing with perlXML processing with perl
XML processing with perl
Joe Jiang
 
Jsonsaga
JsonsagaJsonsaga
Jsonsaga
nohmad
 

Similar to 5 Files Io (20)

C++ io manipulation
C++ io manipulationC++ io manipulation
C++ io manipulation
 
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
 
PHP Presentation
PHP PresentationPHP Presentation
PHP Presentation
 
PHP Presentation
PHP PresentationPHP Presentation
PHP Presentation
 
Phing - A PHP Build Tool (An Introduction)
Phing - A PHP Build Tool (An Introduction)Phing - A PHP Build Tool (An Introduction)
Phing - A PHP Build Tool (An Introduction)
 
Mongokit presentation mongofr-2010
Mongokit presentation mongofr-2010Mongokit presentation mongofr-2010
Mongokit presentation mongofr-2010
 
XML and Web Services with PHP5 and PEAR
XML and Web Services with PHP5 and PEARXML and Web Services with PHP5 and PEAR
XML and Web Services with PHP5 and PEAR
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
Introduction To Lamp
Introduction To LampIntroduction To Lamp
Introduction To Lamp
 
OSS BarCamp Mumbai - JSON Presentation and Demo
OSS BarCamp Mumbai - JSON Presentation and DemoOSS BarCamp Mumbai - JSON Presentation and Demo
OSS BarCamp Mumbai - JSON Presentation and Demo
 
Php performance
Php performancePhp performance
Php performance
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line tools
 
Files IO
Files IOFiles IO
Files IO
 
Zend framework 05 - ajax, json and j query
Zend framework 05 - ajax, json and j queryZend framework 05 - ajax, json and j query
Zend framework 05 - ajax, json and j query
 
Php 3 1
Php 3 1Php 3 1
Php 3 1
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Learning Ruby
Learning RubyLearning Ruby
Learning Ruby
 
The JSON Saga
The JSON SagaThe JSON Saga
The JSON Saga
 
XML processing with perl
XML processing with perlXML processing with perl
XML processing with perl
 
Jsonsaga
JsonsagaJsonsaga
Jsonsaga
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 

5 Files Io

  • 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 + "... "} => ["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 => "dddeeefff" >> g.puts "4th letter to 6th” >> g.rewind >> g.read => "dddeeefff4th letter to 6th"
  • 11.
  • 12.
  • 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

Editor's Notes

  1. The File class is a subclass of the IO class
  2. rewind
  3. make a spec, make it pass