Join Kevin Skoglund for an in-depth discussion in this video Renaming and deleting files, part of Ruby Essential Training.
Now that we know how to read and write from files, let's see how we can rename…our files and delete them.…So I am going to do the renaming and deleting from irb rather than inside a script. …Let's do simple-prompt, open up irb, and I'll create a new file to start…out with that we can rename and then delete when we are done.…So file equals File.new and let's call this file_to_rename.txt and we'll go…ahead and just say we are going to write.…And while we are at it, we'll say puts "This is a file for renaming and then deleting".…
file.close.…All right, so now we've got our file there.…Let's just double-check.…Look at our sandbox, file_to_rename, there it is in my ruby_sandbox.…You want to make sure that where you are located is where it'll be.…So now let's see how to rename it.…It's nice and simple. We just use the File class and call the rename class method.…So File.rename, and we give it the old name as the first argument,…'file_to_rename.txt', and then the second argument will be the new name of the file.…
- 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.