Join Kevin Skoglund for an in-depth discussion in this video Writing to files, part of Ruby Essential Training.
Now that we have seen how we can access files, and how we can open them up…to enable us to write to them, let's see how we can actually write data to the file.…Fortunately, because in the very first movie of this chapter we saw how to do…the basics of input and output, it's going to be super simple.…I want to start out by opening up the irb_ testfile.txt that we created in the last movie.…It should just be a blank file.…But let's type something in there, This is a test.…You can type anything you want.…Save it, close it up, now it has some text in there.…Now I am going to go into irb.…
Notice I am already in my ruby_ sandbox where that file is located,…simple-prompt, and I am going to just instantiate a new file, File.new, and it…will be that file name, irb_testfile.txt.…You want to make sure you type the name correctly otherwise it will create a new file.…What we want to do instead is open that file and actually overwrite what's there.…That's what I want you to see.…So I've instantiated it now.…
- 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?
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.