def current_snapshot
d = nil
for instance in instances do
if instance.id == self.current
d = instance
break
end
end
d
end
Refactorings
No refactoring yet !
Jeffrey Chupp
June 9, 2009, June 09, 2009 14:26, permalink
def current_snapshot
instances.detect{|instance| instance.id == self.current}
end
Martin Plöger
June 20, 2009, June 20, 2009 18:26, permalink
You could also use the alias of detect: find. Not an improvement, but it (at least for me!) reads better. I prefer find to detect. find_all and select both are fine for me.
def current_snapshot
instances.find { |instance| instance.id == self.current }
end
hey folks,
how it will look if we avoid a for loop and try a cool ruby style
i tried out several solutions with reject but i think its not common smart
way. in my code, there a lot of these functionality and each of it takes 8 lines.
maybe you've got a great solution.
best regards and best thanks for you help