I had an array which looked something like this:
arr = [1, 2, 2, 2, 1, 1, 3]
and what I wanted was to group consecutive repeats of the same value together. I can do this with chunk and map.
arr.chunk { |x| x }.map(&:first)
#=> [1, 2, 1, 3]I could use Ruby 2.7's number arguments to maybe make it a little less bizarre:
arr.chunk { _1 }.map(&:first)but that's still really weird looking.
Instead, there is a really nice method Kernel#itself which does nothing but return back the object it's called on. This means I can write the much more beautiful:
arr.chunk(&:itself).map(&:first)