Let’s say I have this each loop in Ruby.
@list.each { |i|
puts i
if i > 10
break
end
}
Where I want to loop through the list until a condition is met.
This stung me as “un Ruby’ish” and since I’m new to Ruby, is there a Ruby way to do this?
You can use
Enumerable#detectorEnumerable#take_while, depending on the result you want.As others have noted, a better style would be to first make your sub-selection and then act on it, e.g.: