需要一些帮助来定义一些方法

时间:2017-03-18 04:59:42

标签: ruby class object

如果我可以获得一些关于打印出用户输入数据的任务的问题(在这个具体的例子中,汽车的年份,型号和品牌),我会非常感激:

# DEFINE YOUR CAR CLASS HERE

# create empty array
array_of_cars = Array.new 

# prompt for and get number of cars
print "How many cars do you want to create? "
 num_cars = gets.to_i

# create that many cars
for i in 1..num_cars 
# get the make, model, and year
puts
print "Enter make for car #{i}: "
make = gets.chomp

print "Enter model for car #{i}: "
model = gets.chomp

print "Enter year of car #{i}: "
year = gets.to_i

# create a new Car object
c = Car.new

# set the make, model, and year
# that we just read in
c.set_make(make)
c.set_model(model)
c.set_year(year)

# add the Car object to the array
array_of_cars << c 
end 

# print out the information for 
# the Car objects in the array
puts
puts "You have the following cars: " 

for car in array_of_cars 
puts "#{car.get_year} #{car.get_make} #{car.get_model}" 
end

我已经拥有该程序的某些部分,但是与它的主要部分斗争,因为我知道该怎么做而不知道如何实现它。

所以对于这部分:#在这里定义你的车类

我明白了:

class Car

def assign(m,n,y)
    #instance variables
    @make = m
    @model = n
    @year = y
end
    #instance methods
def set_make

end

def set_model

end

def set_year

end

def get_make

end

def get_model

end

def get_year

end

首先,我是否正确使用实例变量?

然后,“set”的目的是将值保存到数组中吗?然后“get”允许我稍后提取它们。如果有人能告诉我如何定义其中一个,我想我会理解这个概念。

我知道这似乎有点模糊,所以我会尽力澄清是否会出现一些问题。对于文本墙也很抱歉,谢谢!

1 个答案:

答案 0 :(得分:0)

首先,在“惯用红宝石”中,我们将@variable的getter和setter称为variable(getter)和variable=(setter。)构造函数的名称为{ {1}},而不是initialize

有一个助手可以为类Module#attr_accessor定义,它们在引擎盖下声明getter和setter,这样你的类定义可能就像:

assign

到目前为止一切顺利。其余代码将是:

class Car
  attr_accessor :make, :model, :year

  def initialize(make, model, year)
    @make = make
    @model = model
    @year = year
  end
end
BTW,不是预先创建一个数组,而是可以更好地使用Enumerable#map

array_of_cars = [] # use Array.new with parameter, [] otherwise

# prompt for and get number of cars
print "How many cars do you want to create? "
num_cars = gets.to_i

# create that many cars
(1..num_cars).each do |i| # in idiomatic ruby, never use for loops
  # get the make, model, and year
  puts "Enter make for car #{i}: "
  make = gets.chomp

  print "Enter model for car #{i}: "
  model = gets.chomp

  print "Enter year of car #{i}: "
  year = gets.to_i

  # create a new Car object
  c = Car.new(make, model, year)

  # add the Car object to the array
  array_of_cars << c 
end 

# print out the information for 
# the Car objects in the array
puts
puts "You have the following cars: " 

array_of_cars.each do |car|
  puts "#{car.year} #{car.make} #{car.model}" 
end

这将产生一个开箱即用的阵列。

附录:手动定义getter和setter:

# prompt for and get number of cars
print "How many cars do you want to create? "
num_cars = gets.to_i

# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓  DECLARE IT IN-PLACE
array_of_cars = (1..num_cars).map do |i|
  puts "Enter make for car #{i}: "
  make = gets.chomp

  print "Enter model for car #{i}: "
  model = gets.chomp

  print "Enter year of car #{i}: "
  year = gets.to_i

  # create a new Car object
  Car.new(make, model, year)
end