10

active_support.rbそれが使用するロードプロセスを理解しようとしていました。load_all!autoloadおよびの 3 つの読み込み方法を使用しrequireます。同じファイルに 3 つの異なる読み込み方法を使用するのはなぜですか?

module ActiveSupport
  def self.load_all!
    [Dependencies, Deprecation, Gzip, MessageVerifier, Multibyte, SecureRandom, TimeWithZone]
  end

  autoload :BacktraceCleaner, 'active_support/backtrace_cleaner'
  autoload :Base64, 'active_support/base64'
  autoload :BasicObject, 'active_support/basic_object'
  autoload :BufferedLogger, 'active_support/buffered_logger'
  autoload :Cache, 'active_support/cache'
  autoload :Callbacks, 'active_support/callbacks'
  autoload :Deprecation, 'active_support/deprecation'
  autoload :Duration, 'active_support/duration'
  autoload :Gzip, 'active_support/gzip'
  autoload :Inflector, 'active_support/inflector'
  autoload :Memoizable, 'active_support/memoizable'
  autoload :MessageEncryptor, 'active_support/message_encryptor'
  autoload :MessageVerifier, 'active_support/message_verifier'
  autoload :Multibyte, 'active_support/multibyte'
  autoload :OptionMerger, 'active_support/option_merger'
  autoload :OrderedHash, 'active_support/ordered_hash'
  autoload :OrderedOptions, 'active_support/ordered_options'
  autoload :Rescuable, 'active_support/rescuable'
  autoload :SecureRandom, 'active_support/secure_random'
  autoload :StringInquirer, 'active_support/string_inquirer'
  autoload :TimeWithZone, 'active_support/time_with_zone'
  autoload :TimeZone, 'active_support/values/time_zone'
  autoload :XmlMini, 'active_support/xml_mini'
end

require 'active_support/vendor'
require 'active_support/core_ext'
require 'active_support/dependencies'
require 'active_support/json'

I18n.load_path << "#{File.dirname(__FILE__)}/active_support/locale/en.yml"
4

2 に答える 2

16

Rails が 3 つの異なる読み込み方法 (実際には 2 つ - 以下を参照) を使用する理由は正確にはわかりません。しかし、一般的に、誰かがそうする理由は知っています。

Require「今すぐロードする」という意味です。autoload「必要なときにこれをロードする」という意味です。両方を使用する通常の理由は、すべてのプログラム呼び出しで使用されるとほぼ想定されるいくつかのファイルがあるためです。およびその他はオプションです。たとえば、非推奨のメソッドを使用しない Rails アプリケーションでは、Deprecation;は必要ありません。では、なぜそのファイルをロードして初期設定を遅くするのでしょうか?

また、プログラムの実行の早い段階で必要になるファイルと、待機できるファイルを区別する場合もあります。たとえばGzip、最初のリクエストが来るまでは必要ない可能性が高いです。そのため、オートロードを使用すると、最初のリクエストの速度が若干遅くなりますが、初期設定から時間を短縮できます。

あなたは、なぜautoloadすべてに使用しないのかと尋ねるかもしれません。絶対に必要になる前に何かをロードするのはなぜですか? 理由の 1 つは、オートロードが定数に対してのみ機能することです。したがって、たとえば、active_support/core_extNumeric に一連のメソッドを追加して3.days、 、6.minutes、およびのようなコードを記述できるようにし16.seconds.agoます。には定数3.daysがないため、その式でオートロードをトリガーすることはできません。(基本クラスは既にロードされているため、 autoload はできませんNumeric。追加したいのは単なる拡張機能です。)

最後に、このクラスは実際には 3 つの読み込みメソッドを使用していません。2 つを使用し、1 つ (一種) を提供します。load_all!によって使用さRails::Initializerれます

# Preload all frameworks specified by the Configuration#frameworks.
# Used by Passenger to ensure everything's loaded before forking and
# to avoid autoload race conditions in JRuby.

詳細はわかりませんし、これらの特定のモジュールがプリロードされている理由もわかりません (他のモジュールはプリロードされていません)。ただし、これは特定の環境をサポートするためのものであるため、デフォルトの読み込みメカニズムとは別のコードが必要になる理由がわかります。

于 2009-06-18T16:32:28.793 に答える
1

ruby メソッドを使用して、そのautoload定数を最初に参照したときにロードされるファイル名に定数を関連付けることができます。これにより、起動時にフレームワーク全体をロードする必要がなくなります。

このload_all!メソッドは rails から呼び出され、initializer.rbプリロードされるように構成されたすべてのフレームワークをプリロードするために使用されているようです。load_all!これは、単純に定数の配列を参照する各フレームワークのメソッドを呼び出すことで機能します...これにより、オートロードがトリガーされます。

initializer.rbforのコメントによるとpreload_frameworks...

# Preload all frameworks specified by the Configuration#frameworks.
# Used by Passenger to ensure everything's loaded before forking and
# to avoid autoload race conditions in JRuby.

これrequireは、特定のフレームワークに必要なコア ファイルをロードすることです。

于 2009-06-18T16:33:29.340 に答える