O'Reilly logo

Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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? ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required