In Ruby, it is not necessary to explicitly initialize variables. If a local variable has not been explicitly initialized, it will have the value nil. If this happens unintentionally, though, the variable will not represent an object with the expected methods, and a method call on the variable will raise a NoMethodError.
In the following code, the call to create_file may fail and then the call f.close will raise a NoMethodError since f will be nil at that point.
def dump(x)
f = create_file
f.puts(x)
ensure
f.close
end
References
Everything You Need To Know About Nil
NoMethodError
In Ruby, it is not necessary to explicitly initialize variables. If a local variable has not been explicitly initialized, it will have the value nil. If this happens unintentionally, though, the variable will not represent an object with the expected methods, and a method call on the variable will raise a
NoMethodError.In the following code, the call to
create_filemay fail and then the callf.closewill raise aNoMethodErrorsince f will be nil at that point.References
Everything You Need To Know About Nil
NoMethodError