This one tripped up me all the time when I was just starting out. While the error seems to be about the method, it's usually the object you're trying to call it on that is problematic somehow.
If the object is allowed to be nil, you want to check its presence before calling any method on it. For example:
coffee.drink_itifcoffee.present?
Or if you like concise code:
coffee&.drink_it# shorthand for `coffee && coffee.drink_it`# which basically does the same thing as the present? check
Small nitpick about the example code in article: if my_hash was an actual hash, the error would say {:foo => :bar}:Hash instead of nil:NilClass.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This one tripped up me all the time when I was just starting out. While the error seems to be about the method, it's usually the object you're trying to call it on that is problematic somehow.
If the object is allowed to be nil, you want to check its presence before calling any method on it. For example:
Or if you like concise code:
Small nitpick about the example code in article: if
my_hash
was an actual hash, the error would say{:foo => :bar}:Hash
instead ofnil:NilClass
.