2016-12-08 56 views
0
a = ["Select", "Choice", "Prime"] 
a.each do |f| 
    f.downcase = f.new 
    f.grade = f 
end 

是不是一個辦法做到這一點紅寶石實例化類中的每個

select = Steak.new 
select.grade = "Select" 
choice = Steak.new 
choice.grade = "Choice" 
prime = Steak.new 
prime.grade = "Prime" 

是否有可能在所有實例使用每個幾個類似的類?

或者換個方式嗎?必須能夠更有效地做到這一點。

+1

哪兒了你期望「牛排」來自哪裏? – ndn

+0

除了提供的答案,您可能會對Ruby中的並行賦值感興趣。'a,b,c = [1,2,3]'然後'a#=> 1','b#=> 2'和'c#=> 3'。 –

回答

0

當然,您可以:

grades = ["Select", "Choice", "Prime"] 
steaks = [] 
grades.each do |grade| 
    s = Steak.new 
    s.grade = grade 
    steaks << s 
end 
+0

@BigRon當我使用'.map'時,s將被自動放入結果數組中。我更新了使用'.each'並明確地保存's'。謝謝! –

+0

謝謝!這樣做,我可以訪問像這樣的實例:'steaks [0] .grade',這不是很清楚,即如果我需要比較'steaks [0] .grade thiebo

0

OK,很簡單:

grades = ["Select", "Choice", "Prime"] 
steaks = grades.map do |grade| 
    steak = Steak.new 
    steak.grade = grade.downcase 
    steak 
end 

然而,它確實看起來檔次是牛排的一個重要特徵。也許它應該是這樣的構造函數的一部分?

grades = ["Select", "Choice", "Prime"] 
steaks = grades.map {|grade| Steak.new(grade.downcase) } 

我認爲創建初始化方法可以作爲讀者的練習。

+0

我現在這樣做: 'grades = [「Slect」,「Choice」,「Prime」] names = grades.map {| j | j.downcase} names.zip(grades){| r,n | \t R = Steak.new \t r.grade = N \t r.grade }' – thiebo

+0

@thiebo:這是...一個奇怪的事情。你想用這種'.zip'完成什麼? –

0

如果你想命名每個實例(所以使用散列而不是數組),你可以這樣做:

steaks = {} 
["Select", "Choice", "Prime"].each do |grade| 
    # I think using a symbol as key in this case is nicer 
    steaks[grade.downcase.to_sym] = Steak.new(grade) 
end 

還是有點短/最佳使用地圖就像你問(支持從紅寶石2.1開始):

steaks = ["Select", "Choice", "Prime"].map{ |g| 
    [g.downcase.to_sym, new Steak(grade)] 
}.to_h 

另一種方法是創建一個Butcher類抽象了這一切。

butcher = new Butcher.new 
butcher.addNewSteak(Steak.new("Select")) 
butcher.addNewSteak(Steak.new("Choice")) 
butcher.addNewSteak(Steak.new("Prime")) 

# Or added/created using a loop 
["Select", "Choice", "Prime"].each do |grade| 
    butcher.addNewSteak(Steak.new(grade) 
end 

# Get a list of steaks 
butcher.getAllSteaks() 

# Get steak by grade 
bucher.getSteakByGrade("Select") 

# Get all steaks by grade greater than 
bucher.getSteakGradeGT("Choice") 

屠夫類的實施被故意遺漏。制定是爲了應對亞歷杭德羅·C的答案下提出的問題

0

做的最簡單的方法是什麼,你都在問:

steaks = ["Select", "Choice", "Prime"].map do |grade| 
    s = Steak.new 
    s.grade = grade.downcase 
    s 
end 

在另一方面,我建議grade爲可變成員。可變性,導致錯誤。如果gradeSteak一個基本組成部分,它應該在構造函數初始化:

class Steak 
    def initialize(grade) 
    @grade = grade.downcase 
    end 
end 

那麼你的代碼會更簡單

steaks = ["Select", "Choice", "Prime"].map do |grade| 
    Steak.new grade 
end 
0

通過這種結構,你得到例如哈希作爲鍵的:prime和作爲值的Steak.new("Prime")。 這使得它更容易遍歷所有的牛排,如果你想有一個特定的一個,只需使用steaks[:prime]

class Steak 
    attr_reader :grade 
    def initialize(grade) 
    @grade = grade 
    end 

    def to_s 
    format("Steak %s", grade) 
    end 
end 

steaks = ["Select", "Choice", "Prime"].map{|grade| [grade.downcase.to_sym, Steak.new(grade)]}.to_h 

p steaks 
# {:select=>#<Steak:0x000000013e7000 @grade="Select">, 
# :choice=>#<Steak:0x000000013e6f60 @grade="Choice">, 
# :prime=>#<Steak:0x000000013e6ec0 @grade="Prime">} 

puts steaks.values 
# Steak Select 
# Steak Choice 
# Steak Prime 

puts steaks[:select].grade # => "Select" 

如果你真的想爲你的牛排3個獨立的變量:

select, choice, prime = ["Select", "Choice", "Prime"].map{|grade| Steak.new(grade)}