SlideShare una empresa de Scribd logo
1 de 18
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

Más contenido relacionado

Similar a 5 Files Io

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 Parkpointstechgeeks
 
PHP Presentation
PHP PresentationPHP Presentation
PHP PresentationNikhil Jain
 
PHP Presentation
PHP PresentationPHP Presentation
PHP PresentationAnkush Jain
 
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)Michiel Rook
 
Mongokit presentation mongofr-2010
Mongokit presentation mongofr-2010Mongokit presentation mongofr-2010
Mongokit presentation mongofr-2010namlook
 
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 PEARStephan Schmidt
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operationsmussawir20
 
Introduction To Lamp
Introduction To LampIntroduction To Lamp
Introduction To LampAmzad Hossain
 
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 DemoKetan Khairnar
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line toolsEric Wilson
 
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 queryTricode (part of Dept)
 
The JSON Saga
The JSON SagaThe JSON Saga
The JSON Sagakaven yan
 
XML processing with perl
XML processing with perlXML processing with perl
XML processing with perlJoe Jiang
 
Jsonsaga
JsonsagaJsonsaga
Jsonsaganohmad
 

Similar a 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
 

Último

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Último (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

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

Notas del editor

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