【问题标题】:What is wrong with my Ruby programming code?我的 Ruby 编程代码有什么问题?
【发布时间】:2012-12-15 07:53:42
【问题描述】:

这段代码应该可以工作。但是每次我在 Ruby 上打开它时,Ruby 几乎都会立即关闭,因为它无法读取代码中的某些内容。所以很明显代码中有一个错误,但我查看了所有内容,似乎找不到它。我需要一些帮助才能使此代码正常工作。非常感谢您!

puts 'Enter the number of the current month using the 12 months per year scale. Ex. January would be 1, February would be 2, March would be 3, etc...'
month=gets.chomp
case month
when '1'..'3'
fee='$45'
when'4'..'5'
fee='$55'
when'7'..'8'
fee='$65'
else
fee='$0.00'
end
puts'The fee to apply for the competition is ' + fee + '.00 when you apply on the date of today, ' + Time.now.to_s +'.'
puts 'If your fee came up as $0.00, then that is because the competition has ended. But do not worry, there is always next year!'
sleep 20

【问题讨论】:

  • 在命令行上运行它不会“立即关闭”。这样你就可以看到错误信息了。

标签: ruby syntax syntax-error


【解决方案1】:

这是 Ruby 告诉我的语法错误:

temp.rb:13: syntax error, unexpected tUPLUS, expecting $end
... of today, ' + Time.now.to_s +'.'

+'.' 之间添加一个空格可以修复语法错误。


您可以通过从命令行运行 Ruby 自己解决这个问题。我假设您正在运行 Windows。要进入命令提示符,您可以按 Windows 键R 打开运行对话框。从那里,您可以输入 cmd 并按 enter 以打开命令提示符。然后您可以使用cd 更改当前目录并使用ruby 运行您的Ruby 脚本(假设Ruby 在PATH 中)。您的会话可能如下所示:

C:\Documents and Settings\Madelyn Grewal> cd Desktop

C:\Documents and Settings\Madelyn Grewal\Desktop> ruby myscript.rb
myscript.rb:13: syntax error, unexpected tUPLUS, expecting $end
... of today, ' + Time.now.to_s +'.'

C:\Documents and Settings\Madelyn Grewal\Desktop>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 2021-07-04
    • 2015-06-04
    • 2011-07-28
    相关资源
    最近更新 更多