Few things to change. First of all gets will give you a string together with \n
at the end, so you need to change it to gets.chomp
to remove it.
Now your loop should look like this:
a.each do |e|
break if e == '42'
puts e
end
However ruby's array has much butter function which is perfect for what you want:
puts a.take_while {|e| e != '42'}
Additional notes:
Note that it is operating on strings rather than numbers. You might need to validate the input at some point and convert it into integer values.
5.times do|i|
- the|i|
bit is obsolete.