Prefer in? Over include? for Readable Conditions
When checking if a value exists within a collection, Ruby’s include? method does the job, but Rails provides a more natural alternative through Active Support’s in? method.
Instead of…
…reading your conditions backwards with include?:
nsync = ["Justin", "JC", "Chris", "Joey", "Lance"]
if nsync.include?(candidate)
puts "#{candidate} is in the band"
end
# Or inline
if ["Justin", "JC", "Chris", "Joey", "Lance"].include?(member)
puts "#{member} can join the inevitable reunion tour"
end
Use…
…Rails’s in? method for more natural reading:
nsync = ["Justin", "JC", "Chris", "Joey", "Lance"]
if candidate.in?(nsync)
puts "#{candidate} is in the band"
end
# Reads naturally even inline
if member.in?(["Justin", "JC", "Chris", "Joey", "Lance"])
puts "#{member} can join the inevitable reunion tour"
end
Why?
The in? method reads like English. “Is Justin in NSYNC?” becomes "Justin".in?(nsync). Compare that to nsync.include?("Justin"), which reads as “Does NSYNC include Justin?”—grammatically correct but less intuitive.
The in? method works with anything that responds to include?: arrays, ranges, sets, and strings.
"JC".in?("JC Chasez") #=> true
5.in?(1..10) #=> true
:harmony.in?(Set[:melody, :harmony, :rhythm]) #=> true
It also handles nil gracefully, returning false rather than raising an error:
"Justin".in?(nil) #=> false
Why not?
If you’re not using Rails, you’d need to add activesupport as a dependency. For a single method, that’s probably overkill.
Some teams prefer sticking with Ruby’s standard library to avoid “magic” methods that might confuse developers unfamiliar with Rails conventions. If your collection is already in a well-named variable, nsync.include?(name) reads fine.
Performance is identical—in? simply calls include? on the collection—so choose whichever reads better in context. For inline collections or when the subject of your conditional matters more than the collection, in? wins.
Published on February 2nd, 2026