加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 轨道宝石(一般):宝石如何工作?

发布时间:2020-12-16 21:54:45 所属栏目:百科 来源:网络整理
导读:我一直在使用Rails一段时间,并且一直在我的gemfile中使用宝石,但我从来没有真正了解我安装的gem的功能实际上是如何可用的.说我使用has_permalinks宝石( http://haspermalink.org/).它提供了一个.generate_permalink!我的模型的方法这个方法在哪里定义?如何
我一直在使用Rails一段时间,并且一直在我的gemfile中使用宝石,但我从来没有真正了解我安装的gem的功能实际上是如何可用的.说我使用has_permalinks宝石( http://haspermalink.org/).它提供了一个.generate_permalink!我的模型的方法这个方法在哪里定义?如何才能突然使用这种方法只是安装宝石?是否有某种include / require / load来初始化gem的代码,使其可以被其他应用程序访问?另外,在安装宝石的时候这个代码存储在哪里?

解决方法

我单独回答了你的问题,不合时宜,但我认为这样做可能会使这个顺序更容易理解答案.

Also,where is this code stored when I install the gem?

如果您使用Bundler,您可以执行bundle show_permalink,并将显示该gem的安装位置.这是我用pg gem做的例子:

? bundle show pg
/Users/jasonswett/.rvm/gems/ruby-1.9.2-p320@jason/gems/pg-0.11.0

Where does this method get defined?

如果你做捆绑显示的东西,它返回一个路径 – 该方法被定义在某处. (您可以使用grep -r’def generate_permalink’/ gem / path找到所需的位置.)

How come just I can use this method all of a sudden just by installing
the gem? Is there some sort of include/require/load to initialize the
gem’s code so that it becomes accessible to the rest of the
application?

看看这部分关于Rails初始化过程的文档:
http://guides.rubyonrails.org/initialization.html#config-boot-rb

In a standard Rails application,there’s a Gemfile which declares all
dependencies of the application. config/boot.rb sets
ENV[“BUNDLE_GEMFILE”] to the location of this file,then requires
Bundler and calls Bundler.setup which adds the dependencies of the
application (including all the Rails parts) to the load path,making
them available for the application to load.

看起来像在这个过程中相当早,Rails看着你的Gemfile,并通过Bundler加载所有的宝石.所以有你的包括.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读