Reason #165 • June 14th, 2026

Enumerating objects with Object#itself

Ruby provides Object#itself, a method that simply returns the object it was called on:

Ruby
5.itself
# => 5

"hello".itself
# => "hello"

[1, 2, 3].itself
# => [1, 2, 3]
    

This might seem ridiculous at first glance, but it turns out it pairs well with Symbol#to_proc in some niche cases:

Ruby
[true, false, nil].select(&:itself)
# => [true]

numbers = Array.new(10, &:itself)
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    

It's essentially a more readable alternative to blocks in the shape of { |object| object }.

History

Object#itself was added in Ruby 2.2.0, released on Christmas Day 2014.