如何自动将Gem包含在路径中? [英] How to automatically include Gem in the path?

查看:266
本文介绍了如何自动将Gem包含在路径中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有foo.gem,里面有 lib / foo.rb



gem到Gemfile它的foo.rb在我的路径中是自动需要的。但我需要自动包含它。原因是我在做控制台扩展,我希望它们在没有我写'include Foo'的情况下可用。



我正在试验

  SOME_CLASS.send(:include,Foo)

但不知道要使用哪个类来将它添加到路径中,例如当我开始自动包含的控制台时。这里有一些mixin自动包含在控制台中,我需要我的在那里:)谢谢

  irb(main):006: 0 GT; self.class.included_modules 
=> [PP :: ObjectMixin,JSON :: Ext :: Generator :: GeneratorMethods :: Object,ActiveSupport :: Dependencies :: Loadable,Kernel]

PS



我可以解决初始化程序的问题,但我不想更改项目的代码,我只想添加gem并且它 1.9.3 / Kernel.htmlrel =nofollow> Kernel 模块,它包含在 Object 。这就是出口 puts require 等私有方法的地方因此它是定义命令式API的绝佳选择。



当您扩展 Object 时,人们期望能够在任何对象上显式地调用你的方法,并且他们也明白你的方法依赖于那个对象的状态。

内核方法的理解不同。即使它们在技术上对所有对象都可用,但您不希望人们写出如下内容:

 'some string' .sleep 1000 

这没有意义。 sleep 与字符串无关;它不以任何方式依赖于它。它只能用隐式接收器来调用,就好像 self 这个概念不存在一样。



使您的方法保持私有状态并扩展 Kernel 可以帮助您获取该消息。






您可以在 foo.rb 中做到这一点:

  module Foo 
#...
end

Some :: Class.send:include,Foo

当您<载入 require 某个文件,它是逐行执行的。您可以将任意代码放入文件中的任何位置,即使在模块和类定义中也是如此。您可以充分利用这一点,以便正确设置您的图书馆,以便其他人不需要。


I have foo.gem and there is lib/foo.rb in there.

When I add gem to Gemfile it's foo.rb is automatically required in my path. But I need to include it automatically. Reason for this is I am making console extension and I want them to be available without me writing `include Foo'.

I am experimenting with

SOME_CLASS.send(:include, Foo)

But not sure what class to use to have it added to the path e.g. when I start console that is automatically included. Here are some mixins automatically included in console, I need mine to be there :) Thank you

irb(main):006:0> self.class.included_modules
=> [PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, ActiveSupport::Dependencies::Loadable, Kernel]

P.S.

I can solve problem with initializer but I don't want to change project's code I just want to add gem and that it works.

解决方案

You should use the Kernel module, which is included by Object. It's where private methods like exit, puts and require are defined, so it is an excellent choice for defining an imperative API.

When you extend Object, people expect to be able to call your methods explicitly on any object, and they also understand that your method depends on that object's state.

Kernel methods understood differently. Even though they're technically available to all objects, you don't expect people to write things like:

'some string'.sleep 1000

This makes no sense. sleep has nothing to do with the string; it doesn't depend on it in any way. It should only be called with an implicit receiver, as if the very concept of self didn't exist.

Making your methods private and extending Kernel instead helps you get that message across.


You can do that in foo.rb:

module Foo
  # …
end

Some::Class.send :include, Foo

When you load or require some file, it is executed line by line. You can put arbitrary code anywhere in the file, even inside module and class definitions. You can take advantage of that in order to properly set up your library so that others don't have to.

这篇关于如何自动将Gem包含在路径中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆