Reading Text
To get started reading text in Ruby, follow these steps:
- Enter this code in a new file, gets.rb:
Quote:
print “Please enter the temperature: “
gets
chomp
puts “The temperature is #{$_}.”
|
- Save the file and run it.
- Ruby displays the prompt Please enter the temperature: and then waits for a response from you:
Quote:
C:\rubydev>ruby gets.rb
Please enter the temperature:
|
- Enter a temperature and press Enter. Ruby reads the text that you have entered, chomps the
newline character off the end of it, and displays the resulting text:
Quote:
C:\rubydev>ruby gets.rb
Please enter the temperature: 36
The temperature is 36.
|