What is the difference between using:
require 'digest'
and
load 'digest'
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you
requirethe same file twice, it will be loaded and evaluated only once.load, on the other hand, loads and evaluates the file every time. There are also differences in how actual filename is resolved (thanks, Saurabh).What does this mean practically?
Let’s say we have a library
fooThen we have a file which makes some non-idempotent operations. Say, undefines a method
Then, if we
requiremod.rb twice, nothing bad happens.bargets successfully undefined.But if we
loadmod.rb twice, then secondundefoperation will fail, because method is already gone:There’s no error with
requirebecause in that caseundefhappens only once. Granted, this example is quite contrived, but I hope it illustrates the point.