No magical or strange behavior, p actually refers to Kernel.p
each_slice iterates over a fragment of 2 elements. So:
iteration 1 → n = [1, 2] iteration 2 → n = [3]
The # p kernel writes n.inspect in each iteration, so it writes lines [1, 2] and then [3] , each of which follows a new line.
The # print kernel writes n followed by the value of $, (the default field separator is nil ), so it writes [1, 2] and immediately writes [3]
And finally, Kernel # puts recursively writes each array, followed by a new line. More about this here: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/227540
source share