Exercise 14. Prompting and Passing

Image

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 #{ ...

Get Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition, Video Enhanced Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.