IRb:如何使用预加载的类启动交互式 ruby 会话
当我经历采用 Ruby 语言的旅程时,我花了很多时间在 IRb 上。太棒了!但是,由于我不太了解它的功能,并且仍然是 Ruby 的“笨蛋”,我想知道以下内容:
- 如何在不重新启动 IRb 的情况下“刷新”会话(或者这是不可能的)。
- 如何配置 IRb 以加载一堆源文件“hello.rb”和“hello_objects.rb”,即在启动时加载?我正在大量研究这些类,如果能够知道加载这些类的速记方法,而无需再次手动输入“加载”,那就太好了。
As I am going through my journey by adopting the Ruby language, I spend a lot of time inside IRb. It's just fantastic! But, as I am not very aware of it's capabilities, and still a “nubby” with Ruby, I would like to know the following:
- How can I “flush” the session, without restarting IRb (or is this not possible).
- How can I configure IRb to load a bunch of source files "hello.rb" and "hello_objects.rb", i.e. at startup? I am heavily working in these and it would be nice to know a short hand to load these classes, without manually typing 'load' for each again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定是否可以“刷新”会话。但是,您可以像这样加载您的类:
I'm not sure it's possible to 'flush' a session. However, you can load your classes like this:
您可以在 irb 中管理会话。启动 irb 并尝试以下操作:
还有一个命令“fg(数字)”可以在会话之间切换。
请参阅:http://tagaholic.me/2009/05/11/demystifying -irb-commands.html
You can manage sessions in irb. Start irb and try this:
There is also the command "fg (number)" which switches between sessions.
See: http://tagaholic.me/2009/05/11/demystifying-irb-commands.html
我相信您正在寻找的是修改您的 ~/.irbrc 文件。前面已经提到了,但是没有给出例子。以下是一个简短的示例,说明您在 irb 会话中可能需要一些常用实用程序:
I believe what you're looking for is modifying your ~/.irbrc file. It was mentioned earlier, but no examples given. Here is a short example of requiring some common utilities you may want in your irb session:
您可以将任何 ruby 代码放入 ~/.irbrc 文件中,每当您运行 irb 时都会对其进行评估。在这种情况下,您需要声明。
You can put any ruby code into your ~/.irbrc file, which will get evaluated whenever you run irb. In this case your require statements.
这些不是直接答案,但可以满足您更多了解 irb 的愿望。
控制台对象可以使用许多“irb”方法。
玩得开心。
另一个是“conf”对象,它提供有关您的 irb 环境的反馈:
These are not direct answers but can apply to your desire to understand irb more.
A number of "irb" methods are available to the console object.
Have some fun playing around with those.
Another is the "conf" object that gives feedback about your irb environment: