55502f40dc8b7c769880b10874abc9d0

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

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 !

F551cba44a3c386bf61d6b4d6964b23b

Jeffrey Chupp

June 9, 2009, June 09, 2009 14:26, permalink

1 rating. Login to rate!
def current_snapshot
  instances.detect{|instance| instance.id == self.current}
end
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

June 20, 2009, June 20, 2009 18:26, permalink

1 rating. Login to rate!

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

Your refactoring





Format Copy from initial code

or Cancel