2017-03-18 45 views
0

如果我可以得到一些關於打印出用戶輸入的數據的分配問題的幫助,我將非常感激(在這個具體的例子中,年,模型和使汽車的):需要一些幫助定義一些方法

# 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 

我已經有程序的某些部分,但它的主要部分掙扎,因爲我那種知道該怎麼做,但不是如何實現它。

因此,對於這部分:#定義你的車類HERE

我得到這個:

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 

首先,我才這樣做的權利與實例變量?

然後,「設置」的目的是將值保存到數組中嗎?然後「get」讓我稍後提取它們。我想我會理解這個概念,如果有人能告訴我如何定義其中的一個。

我知道這看起來有點含糊,所以我會盡力澄清是否有問題發生。也很抱歉的文字牆,謝謝你!

回答

0

首先,在「慣用的紅寶石」我們所說的getter和setter @variablevariable(吸氣)和variable=(二傳)構造函數將被命名爲initialize,不assign

有一個幫手,同時定義爲引擎蓋下的聲明getter和setter方法,讓你的類定義可能是短類,Module#attr_accessor

class Car 
    attr_accessor :make, :model, :year 

    def initialize(make, model, year) 
    @make = make 
    @model = model 
    @year = year 
    end 
end 

到目前爲止好。你的代碼的其餘部分將是:

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 

BTW,而不是預先創建一個數組,一個可以更好地使用Enumerable#map

# 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 

這將產生需要開箱的數組。


附錄:定義getter和setter手動:

class Car 
    attr_accessor :make, :model 

    def initialize(make, model, year) 
    @make = make 
    @model = model 
    @year = year 
    end 

    # manually declare getter for year: 
    def year 
    @year 
    end 
    # manually declare setter for year: 
    def year=(year) 
    @year = year 
    end 
end 
+0

你好,首先非常感謝你對你的反應,但是,讓我們說,我不希望使用attr_accessor。在課堂上單獨定義的一個吸氣和吸氣器,更復雜的方法會如何?如果你不介意的話,如果你能演示一個例子,我將不勝感激。例如,如果我有:c.set_make(make),它將如何在類中定義? –

+0

用手動聲明的getter和setter更新了答案。 – mudasobwa

+0

謝謝!先生,祝你有美好的一天 –