Exercise 14. Prompting and Passing
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? ...
Get Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.