gpt4 book ai didi

ruby-on-rails - 学习 RoR,有人可以从 H**ll 解释这个 boot.rb 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 17:27:23 24 4
gpt4 key购买 nike

我正在查看这个 boot.rb 文件:

http://github.com/bestbuyremix/BBYIDX/blob/master/config/boot.rb

在试图理解它之后,就好像到目前为止我什么都没学到。

有人可以详细说明这里发生了什么吗?

我不知道怎么会有人想出这个?

# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb

RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)

module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end

def booted?
defined? Rails::Initializer
end

def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end

def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end

def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end

def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end

class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end

class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
end
end

class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end

def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end

class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end

def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end

def load_rubygems
require 'rubygems'
min_version = '1.1.1'
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end

rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end

def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end

private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end

# All that for this:
Rails.boot!

最佳答案

如果您正在学习 Rails,这不是学习的好地方。也许您有这样的想法,为了使用它,您需要从一开始就了解代码的流程?不要那样做。 :)

如果您正在学习 Rails,请使用许多指南和教程中的任何一个来构建一个基本站点。

至于这段代码,它使用的一些概念涉及ruby的标志性class << self。这是对元类的批判性阅读:http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/

Rails.boot!在底部引导您得出结论“方法 boot! 在对象 Rails 上被调用......回到顶部,您会看到

module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end
...

在这里你可以看到背后的魔力class << self ...它创建了 boot!模块本身的方法。从那里你可以跟踪整个文件的方法调用,因为它检查预初始化文件的存在......

pick_boot返回一个对象,VendorBootGemBoot取决于 vendor_rails? 的结果然后调用run方法。

从那里你有一些标准的类继承 Boot类,因为它设置了其余的库。希望这能让你开始。 :)

这实际上是非常好的 OO 风格……小方法和类都完成一个简单的任务。还有 OO 继承和几个常见的 ruby​​ 习语。总而言之,一段非常好的 ruby​​ 代码。 :)

更新

如果以更程序化的风格编码,下面是它的粗略估计:

RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)unless defined? Rails::Initializer  preinitializer_path = "#{RAILS_ROOT}/config/preinitializer.rb"  load() if File.exist?(preinitializer_path)  if File.exist?("#{RAILS_ROOT}/vendor/rails")    require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"    Rails::Initializer.run(:install_gem_spec_stubs)    Rails::Initializer.run(:set_load_path)  else    begin      require 'rubygems'      min_version = '1.1.1'      rubygems_version = Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion      unless rubygems_version >= min_version        $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)        exit 1      end    rescue LoadError      $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)      exit 1    end    begin      if defined? RAILS_GEM_VERSION         version = RAILS_GEM_VERSION      elsif ENV.include?('RAILS_GEM_VERSION')        version =  ENV['RAILS_GEM_VERSION']      else         version = $1 if (File.read("#{RAILS_ROOT}/config/environment.rb")) =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~=]*\s*[\d.]+)["']/      end      if version        gem 'rails', version      else        gem 'rails'      end    rescue Gem::LoadError => load_error      $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)      exit 1    end    require 'initializer'  endend

关于ruby-on-rails - 学习 RoR,有人可以从 H**ll 解释这个 boot.rb 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4046338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com