mercredi 22 novembre 2017

Ruby each Iterator - Substitute the hash to iterate through based on if condition?

I have two hashes, and need to iterate through one of them based on the result of an if condition. Below is how my code currently looks:

if SOME CONDITION
  hash_a.each do |x|
    some code in here
  end
else 
  hash_b.each do |x|
    the same code in here
  end

There is about 30 lines of code executed for each element, so my question is: is there a way to make the code look something more like this:

SOME CONDITION ? hash_a.each do |x| : hash_b.each do |x|
  some code in here 
end

Or simplify/reduce it in any other way?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire