Why do i keep seeing 'undefined method `x' for nil:nilclass' in ruby?

This error indicates that you're trying to call a method on a nil object. This is a common mistake in Ruby and suggests that something you expected to be an object is actually nil.

Example:


user = nil
name = user.name

Solution:


user = SomeClass.new
name = user.name if user
Always check if an object is nil before calling its methods.

Beginner's Guide to Ruby