以下代码显示错误如下:
"ruby -KU -- 'C:\Users\Ishmael\My Documents\Aptana Studio 3 Workspace\Simple\FirstFile'
C:/Users/Ishmael/My Documents/Aptana Studio 3 Workspace/Simple/FirstFile:55: syntax error, unexpected $end, expecting keyword_end"
我搜索了额外的结尾和结尾的'。'但是徒劳无功,我仍然无法弄清楚它为什么不起作用!
class Greeter
attr_accessor :name
def initialize(name="dudes")
@name=name
end
def say_hi
if @name.nil?
puts "..."
else if @name.respond_to?("each")
@name.each do |name|
puts "Hello #{name.capitalize}!"
end
else
puts "Hi #{@name.capitalize}!"
end
end
def say_bye
if @name.nil?
puts "..."
else if @name.respond_to?("join")
puts 'Goodbye #{@name.join(", ").capitalize}! Come back soon!'
else
puts "Bye #{@name.capitalize}!"
end
end
end
if __FILE__ == $0
greeter = Greeter.new
greeter.say_hi
greeter.say_bye
greeter.name = "Ishmael"
greeter.say_hi
greeter.say_bye
greeter.name = ["Albert", "Brenda", "Charles", "Dave", "Englebert"]
greeter.say_hi
greeter.say_bye
greeter.name = nil
greeter.say_hi
greeter.say_bye
end
答案 0 :(得分:13)
由于缺少end
,它不会抱怨额外的end
,而是文件的过早结束。在这种情况下,您忘记在if __FILE__ == $0
部分之前关闭课程。
这可能是因为您将elsif
与else if
混为一谈,后者创建了一个全新的if
块。