Ruby:未定义的局部变量或方法 'car' -- 修复语法错误



我的程序中有一些语法错误,这些错误真的困扰着我。我似乎不知道如何修复它们,因为我是ruby的新手。第一个错误是在标题上,我肯定还有一些错误。该程序的目的是创建带有品牌、型号和年份的汽车,并让用户输入他们想要的汽车数量,然后在最后显示所有汽车。

有人能给我指正确的方向吗?

这是我的代码:

class Car
def initialize(make, model, year)
@make = make
@model = model
@year = year
end
print "How many cars do you want to create? "
array_of_cars = Array.new  
num_cars = gets.to_i
c = car.new
for i in 1..num_cars 
end
puts
print "Enter make for car #{i}: "
make = gets.chomp
print "Enter model for car #{i}: "
model = gets.chomp
print "Enter year for car #{i}: "
year = gets.to_i
c.set_make(make)
c.set_model(model)
c.set_year(year)
array_of_cars << c 
end 
puts
puts "You have the following cars: " 
for car in array_of_cars 
puts "#{car.get_year} #{car.get_make} #{car.get_model}" 
end

在ruby中,类名是常量,因此应该以大写字母开头,如class Car中所示。当创建该类的新对象时,可以对类本身调用new。因此,您可以将car.new更改为Car.new

您还需要在类中定义set_*get_*方法,但由于这是一种常见的模式,ruby提供了attr_accessor。有关attr_accessor的完整解释,请参见此答案。

请考虑您的Car不做任何事情,它只包含数据,没有方法。当这种情况发生时,考虑将其设为Struct,而不是class。CCD_ 11甚至不指定CCD_。

Car = Struct.new(:make, :model, :year)
array_of_cars = Array.new
while true
puts
print "Enter make for car ('x' to exit): "
make = gets.chomp
break if make == 'x'
print "Enter model for car: "
model = gets.chomp
print "Enter year for car: "
year = gets.to_i
array_of_cars << Car.new(make, model, year)
end
puts
puts 'You have the following cars:' # sorted by year for fun
array_of_cars.sort_by{ | car | car.year }.each do | car |
puts "#{car.year} #{car.make} #{car.model}"
end

一些建议。

使用-w选项运行Ruby:

$ ruby -w cars.rb 
cars.rb:17: warning: mismatched indentations at 'end' with 'for' at 16
cars.rb:34: warning: mismatched indentations at 'end' with 'class' at 1
cars.rb:41: warning: mismatched indentations at 'end' with 'for' at 39

并消除引起警告的原因。

$ ruby -w cars.rb 
How many cars do you want to create? 2
cars.rb:2:in `initialize': wrong number of arguments (given 0, expected 3) (ArgumentError)
from cars.rb:13:in `new'
from cars.rb:13:in `<main>'

new调用initialize,因此new必须具有相同数量的参数作为CCD_ 17中的参数。因此,只有在您询问了所有信息后,才能创建汽车。

不要在课堂上工作。正如所写的,当Ruby读取类定义。对于本练习,您可以将其保留在类定义之外的主级别中,或者将其放入方法中。

for i in 1..num_cars 
end

此循环为空,不执行任何操作。并且更喜欢功能强大的迭代器,而不是这种C、Perl、Java风格(for、while等)。

我用撇号定义字符串,并在需要插值时保留双引号(即使这是纳秒和个人选择的问题)。看看这里和那里。

如果你想熟悉Ruby编程,我推荐The Pickaxe。

Ruby中有很多做事的方法。以下是一个解决方案。

class Car
attr_reader :make, :model, :year
def initialize(make, model, year)
@make  = make
@model = model
@year  = year
end
def self.make_car # class method (more precisely : singleton method)
print 'How many cars do you want to create? '
array_of_cars = Array.new
num_cars = gets.to_i
num_cars.times do | i |
real_index = i + 1
puts
print "Enter make for car #{real_index}: "
make = gets.chomp
print "Enter model for car #{real_index}: "
model = gets.chomp
print "Enter year for car #{real_index}: "
year = gets.to_i
=begin
c = Car.new(make, model, year)
array_of_cars << c 
=end
# some will tell you to avoid unnecessary variables ...
array_of_cars << Car.new(make, model, year)
end
puts
puts 'You have the following cars:' # sorted by year for fun
array_of_cars.sort_by{ | car | car.year }.each do | car |
puts "#{car.year} #{car.make} #{car.model}"
end
end
end # class Car
Car.make_car

相关内容

最新更新