Say I have an array, and I use the keep_if or select methods to delete everything but one object for the array — is there a way to take that object out of the array, so it’s not an array any more?
For example, the way my models are set up, previous and current will only ever have one object.
def more_or_less?(project, current_day)
words = project.words
current = words.select {|w| w.wrote_on == current_day}
previous = words.select {|w| w.wrote_on == (current_day - 1.day)}
end
So, if I want to do a comparison, like if current.quantity > previous.quantity — I’ve resorted to actually writing if current.last.quantity > previous.last.quantity but I’m assuming there’s a more idiomatic way?
If you’re deleting all but one entries, why not use
findto choose the one thing you care about? Something like this:If you’re in ActiveRecord land, then you can use
detectas derp says orto_ato get a plain array that you can usefindon: