lundi 21 mars 2016

Both for & each loops stop after first match when iterating through a hash in ruby

I'm trying to print out the values of a hash where their corresponding keys match a certain condition (if they're odd).

I'm having an issue that's causing my loop to stop working after

The following code returns only the first matching value.

if mycondition
 for key,value in myhash do
   if key.odd?
     return "#{value}"
   end
 end
end

After reading some other questions, I thought return was my issue, but when I take it out, the code returns the entire hash instead of just one matching value.

Anyone know what might be up?

Aucun commentaire:

Enregistrer un commentaire