A Live Developer Journal

Experimenting with Ruby input and file reading and writing

Notes based on Learn Ruby the hard way.

Simple command line form

In this program, we ask the user a series of questions and then return their answers in sentence form.


print "How old are you? "
age = gets.chomp()
print "How tall are you? "
height = gets.chomp()
print "How much do you weigh? "
weight = gets.chomp()

puts "So, you're #{age} old, #{height} tall and #{weight} heavy."

Simple resource links form

Based on the program above, I created a form that asks the user for a name of a resource as well as a link to the resource. Then the resource name and link is printed as a sentence.


print "Resource name: "
resource_name = gets.chomp()
print "Link to resource (url): "
url = gets.chomp()
puts "#{resource_name} (#{url})"

Run file with arguments

This program lets you add variables to your script by adding the values seperated by spaces after the run command for your program. In this case, I ran my program in the command line like this: ruby arguments.rb happy go lucky.


first, second, third = ARGV

puts "The script is called: #{$0}"
puts "Your first variable is #{first}"
puts "Your second variable is #{second}"
puts "Your third variable is #{third}"

If you specify three arguments but only provide one, the variable placeholders will print nothing when the program is run.

Ask something specific

In this program, we provide our name as an argument when we run the program. Then the user is asked a series of questions which are stored as variables within the program. The user is addressed by the name they gave when running the program. The results of the questions are printed as a sentence at the end.


user = ARGV.first
prompt = '> '

puts "Hi #{user}, I'm the #{0} script."
puts "I'd like to ask you a few questions."
puts "Do you like me #{user}?"
print prompt
likes = STDIN.gets.chomp()

puts "Where do you live #{user}?"
print prompt
lives = STDIN.gets.chomp()

puts "What kind of computer do you have?"
print prompt
computer = STDIN.gets.chomp()

puts <<MESSAGE
Alright, so you said "#{likes}" about liking me.
You live in #{lives}. Not sure where that is.
And you have a #{computer} computer. Nice.
MESSAGE

We are using STDIN.gets.chomp() instead of gets.chomp() because if we have passed in arguments from outside of the program, then gets tries to read the first argument passed in as a file and read from that. So you need to use STDIN explicitly when using ARGV.

Read file

In this program, we open a file and tell the user the name of the file and the contents inside the file. Then the user is asked to supply a new filename, and when they enter it the contents of that file is shown. In this case we just gave the same filename again.


filename = ARGV.first

prompt = "> "
txt = File.open(filename)

puts "Here's your file: #{filename}"
puts txt.read()
txt.close()

puts "I'll also ask you to type it again:"
print prompt
file_again = STDIN.gets.chomp()

txt_again = File.open(file_again)

puts txt_again.read()
txt_again.close()



File.open opens a file and returns a File object, but it doesn't return the contents of the file yet. The .read() method reads the content of a whole file. You can pass the file to read as an argument to the function too if you wanted to.

It's important to close files once you are done with them.

Read and write to a file

In this program, we erase a file and then ask the user for three lines which we write individually to the file. Then we close the file after we are done with it.


filename = ARGV.first
script = $0

puts "We're going to erase #{filename}."
puts "If you don't want that, hit CTRL-c (^c)."
puts "If you do want that, hit RETURN."

print "? "
STDIN.gets

puts "Opening the file..."
target = File.open(filename, 'w')

puts "Truncating the file. Goodbye!"
target.truncate(target.size)

puts "Now I'm going to ask you for three lines."

print "Line 1: "; line1 = STDIN.gets.chomp()
print "Line 2: "; line2 = STDIN.gets.chomp()
print "Line 3: "; line3 = STDIN.gets.chomp()


puts "I'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

puts "And finally, we close it."
target.close()

I refactored the target.write code to remove duplication:

target.write(line1, "\n", line2, "\n", line3, "\n")