Premium Content - Free Preview
So far, the Product program used hard-coded code to create and show products from the 'database'. In this challenge, let's turn it into an actual program that can be used by anyone with access to a command line.
When you use
puts in Ruby, it prints text to Standard Output. This is text that is usually shown on a command line or terminal. Standard Input is text that is entered on the terminal that your program can use. It is also the input that is passed in to many of the coding challenges on this site.
It's really simple to get input in Ruby. You just use a method:
gets, which gets a line of input. For example if put this code in a file called greet.rb
puts "Your name?" name = gets puts name
You can then run the program from the command line by entering
ruby greet.rb in its directory. The program will then run, and let you enter your name:
$ ruby greet.rb Your name? Jim Hello Jim
You can use standard string methods to cleanup given input. For example, use
strip to strip off all whitespace from the beginning and end of the input.