短信伙伴 +红宝石咀嚼问题
我正在尝试在 Mac (Snow Leopard) 上使用 Textmate 运行文件 (p015elsifex.rb)
。
当我尝试运行代码时,出现错误,内容如下: NoMethodError:私有方法“chomp”调用 nil:NilClass 在 p015elsifex.rb 的顶层第 6 行
因此它到达第 6 行,并且不要求键盘输入,而是退出。
这是代码:
#elsif example
#Original example
puts "Hello, what's your name?"
STDOUT.flush
name = gets.chomp
puts 'Hello, ' + name + '.'
if name == 'Satish'
puts 'What a nice name!!'
else
if name == 'Sunil'
puts 'Another nice name!'
end
end
# Modified example with elsif
puts "Hello, what's your name?"
STDOUT.flush
name = gets.chomp
puts 'Hello, ' + name + '.'
if name == 'Satish'
puts 'What a nice name!!'
elsif name == 'Sunil'
puts 'Another nice name'
end
# Further modified
puts "Hello, what's your name?"
STDOUT.flush
name = gets.chomp
puts 'Hello, ' + name + '.'
我该如何解决这个问题?
Possible Duplicate:
Why isn't 'gets' working in my Ruby script when I run from TextMate?
Am trying to run a file (p015elsifex.rb)
Using Textmate on Mac (Snow Leopard).
When I try to run the code, I get an error which reads:
NoMethodError: private method 'chomp' called for nil:NilClass
at top level in p015elsifex.rb at line 6
So it gets to line 6, and instead of asking for keyboard input, it just quits.
Here is the code:
#elsif example
#Original example
puts "Hello, what's your name?"
STDOUT.flush
name = gets.chomp
puts 'Hello, ' + name + '.'
if name == 'Satish'
puts 'What a nice name!!'
else
if name == 'Sunil'
puts 'Another nice name!'
end
end
# Modified example with elsif
puts "Hello, what's your name?"
STDOUT.flush
name = gets.chomp
puts 'Hello, ' + name + '.'
if name == 'Satish'
puts 'What a nice name!!'
elsif name == 'Sunil'
puts 'Another nice name'
end
# Further modified
puts "Hello, what's your name?"
STDOUT.flush
name = gets.chomp
puts 'Hello, ' + name + '.'
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Terminal
窗口中而不是在 TextMate 中运行该程序。作为后台任务,它显然没有标准输入。我确信您最终可以将 TextMate 配置为执行您想要的操作,但现在只需保持简单即可。
Run the program in a
Terminal
window instead in TextMate. As a background task it apparently doesn't have standard input.I'm sure you can eventually get TextMate configured to do what you want but for now just keep it simple.