I use in a genealogy program following method to add a mariage/spouse to a Person.
@mariages is an array of arrays.
def add_spouse(spouse, mariage_date = nil, divorce_date = nil)
@mariages.push([spouse, mariage_date, divorce_date]) unless @mariages.index{|(a, b, c)| a == spouse && b == mariage_date}
spouse.mariages.push(self) unless spouse.mariages.index{|(a, b, c)| a == self && b == mariage_date}
end
With the unless @mariages.index{|(a, b, c)| a == spouse && b == mariage_date} i check if the mariage is not allready in the array.
Now i want to keep my mariages in an array of hashes like this
def add_spouse(spouse, mariage_date = nil, divorce_date = nil)
@mariages.push({:spouse => spouse, :mariage_date => mariage_date, :divorce_date => divorce_date}) unless ...
spouse.mariages.push({:spouse => self, :mariage_date => mariage_date, :divorce_date => divorce_date}) unless ...
end
Can someone help me adapt the unless part to do the check if the hash is not allready presant in the array ?
In the block that goes into
indexthe element getting iterated over is a hash so you should useand
PS:
mariageis misspelled. It should be marriage.