SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 

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

Hinweis der Redaktion

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