Today I Learned

A Hashrocket project

Ruby Itself

Today I stumbled across a neat Ruby object, itself. itself returns the receiver, itself.

string = "my string"
string.itself.object_id == string.object_id #=> true

What’s a use case for this object? I used it to divide an array into arrays of matching integers.

 > [1,99,99,1,2,3,2,5].group_by(&:itself)
 => {1=>[1, 1], 99=>[99, 99], 2=>[2, 2], 3=>[3], 5=>[5]}
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.