Skip to content

Instantly share code, notes, and snippets.

@ilake
Last active December 19, 2015 17:19
Show Gist options
  • Save ilake/5990129 to your computer and use it in GitHub Desktop.
Save ilake/5990129 to your computer and use it in GitHub Desktop.

reference

load

  • load 了馬上就會進來

autoload

  • 據說要用時才會進來 不過經我測試, 在irb 上 ruby-2.0.0-p195, ruby-1.9.3-p392 還是一autload 就進來
  > autoload(:Foo, "./foo.rb")
  => nil
  > defined?(Foo)
  => "constant"
  • 主要會用在想加速啟動流程, 省時間的時候, 像是thin 之類的application

Thus autoload() is safe to use only when there is a single,uniform place where a constant is meant to be defined; it cannot be used to incrementally build up class or module definitions from several different source files.


## require 
 - 不會重複require

require './autoload.rb' => true require './autoload.rb' => false

- require() is that you can omit the file extension when loading your code. Thus require("./calendar") will work just as well as require("./calendar.rb").

## require_relative

relative paths would be evaluated relative to wherever you executed the files from, not relative to where the files live on the file system 在執行程式時, 相對路徑是從檔案執行的地方開始, 而不是檔案的位置 所以才會看到像是, 這種把檔案位置path load 進來的作法 $LOAD_PATH.unshift("#{File.dirname(FILE)}/.") 不過這種作法會把整目錄弄進來, 可能會造成一些conflict 所以也會看到這種作法 require "#{File.dirname(FILE)}/../lib/calendar"

所以就出現了 require_relative 反正就是從檔案相對位置load


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment