J'essaie ruby en faisant un programme dont j'ai besoin. J'ai une classe personnalisée, et j'ai besoin d'un tableau d'objets de cette classe. Cette classe personnalisée a certains attributs qui changent au cours du programme. Comment puis-je trouver un objet spécifique dans mon tableau, pour y accéder et le modifier?Ruby tableau objet trouver
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
Comment faire vous identifiez l'objet Mathima auquel vous souhaitez ajouter quelque chose? Et quel est ce "quelque chose", est-ce le contenu de tmima? –
merci à tous. J'ai utilisé un hachage pour mathimata et ça marche très bien. – pvinis