Basic Input/Output


Output

Ruby uses a simple interface for gathering inputs and printing outputs. The commandsputs and gets stand for "put string" and "get string" respectively. As you've already seen, puts prints to the standard output with a newline character at the end. The alternative print, prints to the standard output without the newline character. In most cases, puts is the appropriate choice for printing the the screen. However, it can be useful to print to the screen but keep the cursor on the same line. As the example below demonstrates, we'll define a prompt variable that looks like an arrow, '-->', which we want to appear immediately before the cursor to collect input. Also, don't forget there is yet another method for printing text, p. This method is unique in that it's designed to print for inspection.

Input

To retrieve input from a user Ruby has gets and gets.chomp. The difference between the two is that "chomp" will remove the newline character. The most common way to ask for user input is setting a variable to gets.chomp. For example:

>> input = gets.chomp
# ruby interpreter pauses to allow input
hey
"hey"
>> print input
"hey"

By adding the ability to collect user input with everything you've learned thus far, you should now be able to create an interactive command line program. Here is a snippet from a simple command line program I created. The program prompts the user for a book title and rating and then appends that information onto the master list along with a timestamp.

require 'date'
timestamp = DateTime.now
prompt = '-> '

# collect book name
puts 'Enter a new book' 
print prompt
book = gets.chomp


# collect book rating
puts 'What rating would you give ' + book + '?'
puts 'scale from 1 - 5'
print prompt
rating = gets.chomp

# comment on the book rating
if rating.to_i < 3 
    puts 'Wow, I wont be adding that to my reading list!'
elsif rating.to_i == 3 
    puts 'That sounds like a mediocre book.'
else 
    puts 'Oh! Sounds like I need to read that book!'
end

# write the book and rating to a file called 'books.rb'
puts "good job, that's all for now"
file = File.open('bookshelf/books.rb', 'a') do |file|
file << " Book: " + book.capitalize + "," + " Rating: " + rating  + ", " + "Date: " + timestamp.strftime('%D') +  " \n"
file.close
end

Read through the program twice. You should understand about half of the code with the exception of writing to the 'books.rb' file at the end. The first line,require 'date' is needed to bring in the Ruby date class, allowing us to create a timestamp on the second line. You should be able to create your own command line program that prompts for input and then responds accordingly. Try creating a similar program with the following:

  • collect user input
  • create at least two possible responses based on the user input
  • respond accordingly

Summary

In this chapter we learned the basic interface for Ruby's input(gets) and output(puts). We use "puts" to put a string to the screen and "gets" to get a string as input. To remove the newline from "gets" use gets.chomp.

results matching ""

    No results matching ""