When to use `require`, `load` or `autoload` in Ruby?

前端 未结 4 963
死守一世寂寞
死守一世寂寞 2020-12-02 12:06

I understand the subtle differences between require, load and autoload in Ruby, but my question is, how do you know which one to use?

4条回答
  •  暖寄归人
    2020-12-02 12:39

    here's what you gain with autoload over require:

    autoload is primarily for speeding up the initialization phase of your Ruby program or Rails application. By not loading the resources until they are needed, it can speed up things quite a bit.

    Another advantage is that you may not need to load some parts of the code, if the user doesn't use certain features -- thereby improving load time and reducing the memory footprint.

提交回复
热议问题