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

我一直在使用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加载所有的宝石.所以有你的包括.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...