Use Hash#fetch over Hash#[]
Instead of allowing nil objects to be passed around your code, force throwing KeyError
exception or provide default argument:
def employees_names
params[:employees].map(&:name)
end
becomes
def employees_names
params.fetch(:employees).map(&:name)
rescue KeyError
[]
end
or even
def employees_names
params.fetch(:employees, []).map(&:name)
end
Reference: Brain Pratt's Blog
Written by Adam Stankiewicz
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#