consider the following code
def salt
[]
end
def pepper
salt = salt.map{ |grain| 'ok' }
end
Gives the following result:
NoMethodError (undefined method `map' for nil:NilClass):
Why do these circumstances exactly cause an error? This is unexpected.
Because your local variable
saltis shadowing the methodsalt. Since the variablesalthas been just been defined, its value isnil. You can remedy this by explicitly callingsalton the instance to get the method: