Join Kevin Skoglund for an in-depth discussion in this video Reading from files, part of Ruby Essential Training.
Now that we know how to write data to files, it's time to see how to read data…back from files, and fortunately it's just as easy.…In the last movie, I wrote the irbtestfile.txt and I just put the alphabet from…a to p with a line break after the d. So that's what we are looking at.…That's the text we're going to be working with. I'll close that up.…Let's go back into irb, notice I'm in my ruby_sandbox, simple-prompt.…So let's go ahead and just instantiate a new file.…I am going to use the instantiation method because I want to step through the process.…
I don't want to execute the whole block of code at one time.…irbttestfile.txt and then read.…We are going to read back from this time, not write.…Write would overwrite what we have there; instead we want to read.…So now, if we used puts to put information into the file, then guess what?…We use gets to get a line back out of the file and notice it includes the line return.…It is in fact the mirror image of puts.…Puts a line plus a line return; gets gets a line plus the line return.…
- Using Ruby in the Interactive Ruby Shell and in standalone scripts
- Learning to write custom code blocks to find, merge, and sort
- Using modules for namespacing or as mix-ins
- Reading from and writing to files
- Creating a full Ruby project from start to finish
Skill Level Beginner
1. Getting Started with Ruby
2. Ruby Object Types
3. Control Structures
4. Code Blocks
8. Working with Files
9. Ruby Project: Creating the Food Finder
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.