Ruby the Hard Way: Asking Questions

Time time has come to let users input data into our program.

Using gets.chomp allows us to save a user’s input and lets us assign it to a variable and print it out back to the user like:


print “How old are you? ”   #print instead of put, doesnt add another line to the end of the                                                              output 

age = gets.chomp   #saves input ( strings ) under variable of age

print “How tall are you? ”

height = gets.chomp

print “How much do you weigh? ”

weight = gets.chomp

print “What’s your favourite CS:GO team?”

team = gets.chomp

puts “So, you’re #{age} old, #{height} tall and #{weight} heavy.”

puts “You also like #{team}, thats my favourite team too”





Saving integers


print “Give me a number: ”
number = gets.chomp.to_i   #saves the input ( string ) and converts it to integers

bigger = number * 100

puts “A bigger number is #{bigger}.”

print “Give me another number: ”

another = gets.chomp

number = another.to_f    #saves the input ( string ) and converts it to integers with decimals                                                             like 1.1

smaller = number / 100

puts “A smaller number is #{smaller}”

print “Give me your money, and i’ll give 10% back: ”

number_2 = gets.chomp.to_f

print “Okay, i give you #{number_2 / 100 * 10} back”   #asks for a number and gives 10%                                                                                                                           back to the user



Parameters, Unpacking, Variables


first, second, third = ARGV

puts “Your first variable is: #{first}”

puts “Your second variable is: #{second}”

puts “Your third variable is: #{third}”


print “What’s your name?”

name = $stdin.gets.chomp   #gets.chomp has problems with ARGV

print “Ah your name is #{name}”


ARGV: is the “argument variable” , it holds the arguments users pass to it when starting the ruby script like:

ruby ex.13 I am cool

Your first variable is: I
Your second variable is: am
Your third variable is: cool

ARGV assigns the arguments to the three variables above in order from left to right.



Prompting and Passing


user_name = ARGV.first #takes the first input
prompt = ‘> ‘   #indicates user input field

puts “Hi #{user_name}.”

puts “I’d like to ask you a few questions.”

puts “Do you like me #{user_name}?”

puts prompt

likes = $stdin.gets.chomp

puts “Where do you live #{user_name}? ”

puts prompt

lives = $stdin.gets.chomp

# a comma for puts is like using it twice

puts “What kind of computer do you have? “, prompt

computer = $stdin.gets.chomp

puts “””

Alright, so you said #{likes} about liking me.

You live in #{lives}. Not sure where that is.

And you have a #{computer} computer. Nice.




Reading files…


filename = ARGV.first   #takes the first input argument

txt = open(filename)   #reads a seperate .txt file when adding the name of the .txt file to                                               initial start of script

puts “Here’s your file #{filename}: ”

print   #prints whats written in .txt file

print “Type the filename again: ”

file_again = $stdin.gets.chomp

txt_again = open(file_again)   #open is basically reading, whats inside of another file

print   #print what was read in that file


and writing files


The most used methods to remember:

  • close — Closes the file. Like File->Save.. in your editor.
  • read — Reads the contents of the file. You can assign the result to a variable.
  • readline — Reads just one line of a text file.
  • truncate — Empties the file. Watch out if you care about the file.
  • write('stuff') — Writes “stuff” to the file.



filename = ARGV.first

puts “We’re going to erase #{filename}”

puts “If you dont want that, hit CTRL-C (^C).”

puts “If you do want that, hit RETURN.”


$stdin.gets   #accepts ENTER/RETURN

puts “Opening the file…”

target = open(filename, ‘w’)   #w(rite) is explicitely indicating that we want to write                                                                       something to our file

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                      #saves input

print “line 3: ”

line3 = $stdin.gets.chomp

puts “I’m going to write these to the file.”



target.write(line2)     #adds these lines to our external file





puts “And finally, we close it.”




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s