This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
The File class handles file input and output in Ruby. When working with files in a programming language, there are different things you need to tell the computer to do in order to assure that files are correctly handled on the disk. There are different modes that files can be opened as well: reading, writing, and different combinations of both of those.
Links
Code Samples
In this example, we open and write to a file:
File.open("example.txt", "w") do |file|
print "Enter your name: "
name = gets.chomp
file.puts "Name: #{name}"
print "Enter your email: "
email = gets.chomp
file.puts "Email: #{email}"
end
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up