December 2014
Beginner
336 pages
7h 41m
English
Let’s do one exercise that uses ARGV and gets.chomp together to ask the user something specific. You will need this for the next exercise, where we learn to read and write files. In this exercise we’ll use gets.chomp slightly differently by having it print a simple > prompt. This is similar to a game like Zork or Adventure.
ex14.rb
1 user_name = ARGV.first 2 prompt = '> ' 3 4 puts "Hi #{user_name}." 5 puts "I'd like to ask you a few questions." 6 puts "Do you like me #{user_name}? ", prompt 7 likes = $stdin.gets.chomp 8 9 puts "Where do you live #{user_name}? ", prompt 10 lives = $stdin.gets.chomp 11 12 puts "What kind of computer do you have? ...
Read now
Unlock full access