私はRubyを試していますが、私は必要なプログラムを作っています。私はカスタムクラスを持っており、そのクラスのオブジェクトの配列が必要です。このカスタムクラスには、プログラムの過程で変化するいくつかの属性があります。 アレイ内の特定のオブジェクトを見つけると、そのオブジェクトにアクセスして変更することはできますか?Ruby配列オブジェクトfind
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の内容ですか? –
ありがとうございます。私はmathimataのためのハッシュを使用して、それはうまく動作しています。 – pvinis