How to check in an elegant way if an object is in a collection
Usually we use Collection#include? method on Rails when checking if a object is part of a collection.
You can do this in a different way and elegant:
life_characters = %w(Pinkman Walter)
"Hank".in? life_characters #=> false
"Walter".in? life_characters #=> true
The argument of in? method should respond to include?
Written by Rui Anderson
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Related Tags
#rails
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#