Join Kevin Skoglund for an in-depth discussion in this video Accessing files, part of Ruby Essential Training.
- View Offline
- Exercise Files
Now that we have talked about the basics of working with the file system, and…we've seen how we can create paths to the files that we want to work with, it's…time to see how we actually go about accessing those files.…How do you open them up for reading and writing?…There are two different ways that we can do it, and we'll take a look at both of them.…The first is with File.new and the second one is File.open.…Let's look at File.new in irb.…So I will open up a new irb session with simple-prompt, and notice that I am…inside my ruby_sandbox.…That's important, because that's where things will be saved.…
That's where my files will go.…The first approach, File.new, is instantiating a new file object, like you would think.…So we want to assign that to a variable, just like we normally do when we…instantiate an object.…File.new wants two arguments.…The first is the filename that we either want to read from or write to.…We are going to create a new file and write to it.…I am going to call it irb_testfile.txt.…
- 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.