0

我正在关注 Learn Ruby The Hard Way 第 14 章。我自己输入了教程中的内容。我什至尝试复制和粘贴教程本身的内容。我的文本文件 ex.rb 具有以下内容:

user = ARGV.first
prompt = '> '

puts "Hi #{user}, I'm the #{$0} script."
puts "I'd like to ask you a few questions."
puts "Do you like me #{user}?"
print prompt
likes = STDIN.gets.chomp()

puts "Where do you live #{user}?"
print prompt
lives = STDIN.gets.chomp()

puts "What kind of computer do you have?"
print prompt
computer = STDIN.gets.chomp()

puts <<MESSAGE
Alright, so you said #{likes} about liking me.
You live in #{lives}.  Not sure where that is.
And you have a #{computer} computer.  Nice.
MESSAGE

该教程说我应该得到以下输出:

$ ruby ex14.rb Zed
Hi Zed, I'm the ex/ex14.rb script.
I'd like to ask you a few questions.
Do you like me Zed?
> Yes
Where do you live Zed?
> America
What kind of computer do you have?
> Tandy
Alright, so you said Yes about liking me.
You live in America.  Not sure where that is.
And you have a Tandy computer.  Nice.

我收到两个错误。他们来了:

$ ruby ex.rb Zed
ex.rb:19: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '
('
Alright, so you said #{likes} about liking me.
               ^
ex.rb:20: syntax error, unexpected keyword_in, expecting end-of-input
You live in #{lives}. Not sure where that is.

关于发生了什么的任何想法?

4

1 回答 1

3

从你的问题中获取文件对我来说也很好。

但是,当我更改puts <<MESSAGE为:

puts << MESSAGE

即在 << 和 MESSAGE 之间有一个空格,我得到了你的错误。

当您将文件复制/粘贴到此处时,您的文件中必须有一些字符在该位置丢失。

于 2014-04-17T13:25:50.467 回答