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]}
Tweet