扩展IRB主要方法
我的应用程序中有一个目录结构。 出于开发目的(可能超出),我目前有一个类X
,它有类方法pwd
, cd
和ls
。 当我在我的应用程序中输入irb
whith时,有没有办法让这些方法可用,例如:
2.1.5 :0 > pwd /current_dir/
目前我在做:
2.1.5 :0 > X.pwd /current_dir/
这简直不方便。
我可以简单地在现有类中添加内容的解决方案将是完美的,例如:
class X < Irb::main def self.pwd #stuff end end
现在我没有真正挖掘hirb
,但如果有一个与hirb
或irb
一起使用的解决方案,我会hirb
! 谢谢你的帮助!
在Rails中,当通过IRB启动Rails应用程序时,您可以有条件地将方法混合到控制台中。
这是使用application.rb
文件中的console
配置块完成的。
module MyApp class Application < Rails::Application # ... console do # define the methods here end end end
在您的情况下,有几种可能性。 您可以简单地将方法委派给您的库。
module MyApp class Application < Rails::Application console do # delegate pwd to X def pwd X.pwd end end end end
或者如果X是模块,您可以包含它
module MyApp class Application < Rails::Application console do Rails::ConsoleMethods.send :include, X end end end