ruby require include的区别

  • “require” 可載入某個 a.rb 檔案, 且可以省略 ”.rb”. 而且它只會在第一次的時候載入, 若再次 “require” 時就會忽略

    require 'a'

    a
    = A.new
  • “load” 和 “require” 一樣但要用 a.rb 全名, 且每次一定會重新載入
    load 'a.rb'

    a
    = A.new


  • 載入程式庫的順序呢(類似 java class path)? Ruby 把這個資訊存在 ”$:” 系統全域變數上, 你可以藉著 RUBYLIB 或 ruby -I 來加入新的載入目錄.
    puts $:


  • “include” 用來 mix-in 某個模組, 可以減少書寫的長度
    require 'webrick'
    include WEBrick

    //可以不用 server = WEBrick::HTTPServer.new(...)
    server = HTTPServer.new(...)


原文地址:https://www.cnblogs.com/forward/p/1641972.html