让我们使用ARGV
和gets.chomp
一起来向使用者提一些特别的问题。下一节练习你将会学习到如何读写文件,这节练习是下节的基础。在这道练习里我们将用一个简单的>
作为提示符号。这和一些游戏中的方法类似,例如 Zork 或者 Adventure 这两款游戏。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span class="nv">user</span> <span class="o">=</span> ARGV.first <span class="nv">prompt</span> <span class="o">=</span> <span class="s1">'> '</span> puts <span class="s2">"Hi #{user}, I'm the #{$0} script."</span> puts <span class="s2">"I'd like to ask you a few questions."</span> puts <span class="s2">"Do you like me #{user}?"</span> print prompt <span class="nv">likes</span> <span class="o">=</span> STDIN.gets.chomp<span class="o">()</span> puts <span class="s2">"Where do you live #{user}?"</span> print prompt <span class="nv">lives</span> <span class="o">=</span> STDIN.gets.chomp<span class="o">()</span> puts <span class="s2">"What kind of computer do you have?"</span> print prompt <span class="nv">computer</span> <span class="o">=</span> STDIN.gets.chomp<span class="o">()</span> puts <span class="s"><<MESSAGE</span> <span class="s">Alright, so you said #{likes} about liking me.</span> <span class="s">You live in #{lives}. Not sure where that is.</span> <span class="s">And you have a #{computer} computer. Nice.</span> <span class="s">MESSAGE</span> |
