I just wrote a tiny gem called in_enumerable. It extends Ruby’s Object type with the tasty ‘in?’ method, which returns true if an object is included in a list or other enumerable value.
So you can do this:
1.in? [1,2] # => true 3.in? [1,2] # => false
instead of the slightly more awkward:
[1,2].include?(1) # => true [1,2].include?(3) # => false
Despite its name, in_enumerable doesn’t require an enumerable type. It uses duck typing to work with any type that has an ‘include?’ method, such as Array, Hash, String, Range, Set, and even Module.
gem install in_enumerable
require 'rubygems' require 'in_enumerable'
I noticed that typing ri Object#in? reveals that a similar method supposedly already exists on the Object type. And if you google it you can find the source code for it. (It’s almost the same as mine.) But it doesn’t actually seem to be implemented, at least not in Ruby 1.8.6. So I made my own.