Console
Programs are often run with console input and output. We use the puts()
, print()
and p()
methods in Ruby to display text (or other data types).
With stdin, we handle input from the console. Operators are helpful. And often a loop is used to create an interactive console program with a prompt.
We begin with a simple program—here the puts()
method writes a line to the console window. It writes each argument to a separate line.
String
constructor to convert integers to strings.value = 100 # Print values on separate lines. # ... Parentheses are optional. puts value puts("FINISHED") # Use << to append a string. puts "VALUE " << String(value) # Use + to append. puts "VALUE " + String(value) # Print all Array elements on separate lines. elements = [10, 100, 1000] puts elements100 FINISHED VALUE 100 VALUE 100 10 100 1000
Let us continue with the P method. This one is good for lazy programmers. It displays data in a literal way—it includes quotes around a string
, for example.
# Use p method. p "cat" # Write an array. items = [5, 55, 555] p items # Write a hash. lookup = {"cat" => 4, "bird" => 2} p lookup # Use p to write two strings on one line. part1 = "HELLO" part2 = "WORLD" p part1 << "... " << part2 # Nil is displayed as nil. p(nil)"cat" [5, 55, 555] {"cat"=>4, "bird"=>2} "HELLO... WORLD" nil
This method appends no newlines to text. We can thus use many print statements, one after another, on a single line. But we must also print a newline manually.
Print()
is a good choice for lines that are built up in many steps. We can avoid concatenating strings ourselves.# Print statements on the same line. print "dog" print " is cute" print "\n" # Print entire-line statements. print "There are " << String(4) << " apples.\n" print "I ate a lemon.\n" # Print a multiline statement. print "***\nYou are a winner!\n***\n"dog is cute There are 4 apples. I ate a lemon. *** You are a winner! ***
We can read a line from the console with $stdin.readline
. This method returns a string
. The stdin source can be configured but by default it is set to the keyboard on the console.
# Read line from console window. line = $stdin.readline() # Display the string. puts "You typed: " << linecat You typed: cat
An interactive console program can be developed with $stdin.readline
. First we enter a while-true
loop—this continues indefinitely.
while true print "Type a number: " line = $stdin.readline() begin # Convert string to integer. number = Integer(line) # Handle various cases. case number when 0 puts "Zero" when 1, 2, 3 puts "One to three" else puts "Other" end rescue # Let the loop continue. puts "Invalid" ensure puts "Done" end endType a number: 0 Zero Done Type a number: 2 One to three Done Type a number: X Invalid Done
Ruby, as an interpreted language, is well-suited to developer or server tasks. For user programs, a compiled language is often a better choice.
But for developers, console programs are easy-to-maintain and efficient. With print()
, puts()
and the concise p()
method, we easily output text to an output stream.