This is a common Ruby error which indicates that the method or attribute for an object you are trying to call on an object has not been defined.
NoMethodError: undefined method SOME_METHOD for nil:NilClass
For example, the
String class in Ruby has the method
size (which is synonymous with
length, so I can write...
greeting = "hello" greeting.size #=> 5
loveliness does not exist for a string, so when I type it, I will get...
NoMethodError: undefined method loveliness for nil:NilClass
I find this will come up when I think I'm operating on an object with methods, but I'm actually operating on a hash with attributes.
my_hash[:loveliness] # "very lovely" yay, this is a thing that exists! my_hash.loveliness # NoMethodError: undefined method loveliness nil:NilClass
Of course, because this is Ruby, we can actually define
loveliness if we wanted to very easily by monkeypatching the
class String def loveliness "very lovely" end end
Now we know how lovely our string is. If I was mistaken or unclear, please feel free to add a comment to clarify anything here.
Happy coding ❤️