我正在嘗試製作我需要的程序。我有一個自定義類,我需要該類的一個對象數組。這個自定義類有一些在程序過程中發生變化的屬性。 如何在數組中找到特定對象,以便我可以訪問它並進行更改?Ruby數組對象查找
class Mathima
attr_accessor :id, :tmimata
def initialize(id)
@id = id
@tmimata = []
end
end
# main
mathimata = []
previd = id = ""
File.read("./leit/sortedinput0.txt").lines do |line|
array = line.split(' ') # i am reading a sorted file
id = array.delete_at(0) # i get the first two words as the id and tmima
tmima = array.delete_at(0)
if previd != id
mathimata.push(Mathima.new(id)) # if it's a new id, add it
end
# here is the part I have to go in mathimata array and add something in the tmimata array in an object.
previd = id
end
怎麼辦你確定你想添加什麼Mathima對象?那是什麼「東西」,它是tmima的內容嗎? – 2010-11-22 13:43:25
謝謝大家。我用mathimata的散列,它工作得很好。 – pvinis 2010-11-24 07:09:43