Ruby 我无法执行这个程序
错误:C:\Users\RR\Desktop\ruby\u sandbox>ruby classes.rb classes.rb:44: 语法错误,输入意外结束,应为关键字\u end 我的代码是:Ruby 我无法执行这个程序,ruby,Ruby,错误:C:\Users\RR\Desktop\ruby\u sandbox>ruby classes.rb classes.rb:44: 语法错误,输入意外结束,应为关键字\u end 我的代码是: class Animal attr_accessor :name,:age,:sex,:location def initialize(age=18,sex="not available",location="not specified") puts "details of animal
class Animal
attr_accessor :name,:age,:sex,:location
def initialize(age=18,sex="not available",location="not specified")
puts "details of animal"
@age=age
@sex=sex
@location=location
end
def condition(age,name)
if animal.age>animal1.age
puts "#{animal.name } is older than #{animal1.name}"
else
puts "animals age are in increasing order"
end
end
在编写ruby时,请注意缩进。您将看到您遗漏了
end
关键字的地方
但在您的情况下,问题不仅仅在于开始修复之前的缩进。您可以查看一些文档,或者在您的案例中提供帮助
祝你好运。你应该在结尾处添加
end
,谢谢你的帮助。让我来看看。另外值得注意的是,animal1
是你提到的东西,但从未在任何地方定义过。但是请告诉我,为什么需要额外的“end”为什么要添加“extra”end?因为语言需要它?您必须“结束”类声明,而不仅仅是方法定义。Ruby告诉您:“语法错误,输入意外结束,需要关键字\u end”意味着Ruby需要一个结束。您在错误中显示给我们的行号不正确,并且与您提供给我们的代码不匹配,因为它只有21行。在请求帮助时,错误和代码的一致性非常重要。适当的缩进是不够强调的。当你在代码错误上变得马虎时,就会发生错误,而这些错误往往是非常愚蠢的,通过保持整洁本可以避免。你是对的@tadman…..谢谢你再次提供这些教程。。。