key = "41521"
key.each_char.each_cons(2).map { |a| a.join.to_i }
#=> [41, 15, 52, 21]
or
key.gsub(/\d(?=(\d))/).with_object([]) { |s,a| a << (s<<$1).to_i }
#=> [41, 15, 52, 21]
or
a = []
e = key.each_char
#=> #<Enumerator: "41521":each_char>
loop { a << (e.next << e.peek).to_i }
a #=> [41, 15, 52, 21]
In #3 Enumerator#peek raises a StopInteration exception when the internal position of the enumerator is at the end (unless key is an empty string, in which case Enumerator#next raises the StopInteration exception). Kernel#loop handles the exception by breaking out of the loop.
arraycontains 4 elements. In addition, your code contains excessive repetition. For example, you could write(0..key.size-2).each_with_object([]) { |i,array| array << key[i,2].to_i } #=> [41, 15, 52, 21]. Note that Enumerable#each_with_object merely serves to eliminate the need forarray = []at the beginning anarray(to return the computed value ofarray) at the end.