IRb:如何使用预加载的类启动交互式 ruby​​ 会话

发布于 2024-08-29 03:20:05 字数 255 浏览 5 评论 0原文

当我经历采用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

杀お生予夺 2024-09-05 03:20:05

我不确定是否可以“刷新”会话。但是,您可以像这样加载您的类:

irb -r 'hello.rb' -r 'hello_objects.rb'

I'm not sure it's possible to 'flush' a session. However, you can load your classes like this:

irb -r 'hello.rb' -r 'hello_objects.rb'
过气美图社 2024-09-05 03:20:05

您可以在 irb 中管理会话。启动 irb 并尝试以下操作:

x=1
irb     # Opens a new session
puts x  # error
jobs    # lists sessions
quit    # kills current session
puts x  # 1

还有一个命令“fg(数字)”可以在会话之间切换。
请参阅:http://tagaholic.me/2009/05/11/demystifying -irb-commands.html

You can manage sessions in irb. Start irb and try this:

x=1
irb     # Opens a new session
puts x  # error
jobs    # lists sessions
quit    # kills current session
puts x  # 1

There is also the command "fg (number)" which switches between sessions.
See: http://tagaholic.me/2009/05/11/demystifying-irb-commands.html

野の 2024-09-05 03:20:05

我相信您正在寻找的是修改您的 ~/.irbrc 文件。前面已经提到了,但是没有给出例子。以下是一个简短的示例,说明您在 irb 会话中可能需要一些常用实用程序:

# Print to yaml format with "y"
require 'yaml'
# Pretty printing
require 'pp'
# Ability to load rubygem modules
require 'rubygems'
# Tab completion
require 'irb/completion'

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:

# Print to yaml format with "y"
require 'yaml'
# Pretty printing
require 'pp'
# Ability to load rubygem modules
require 'rubygems'
# Tab completion
require 'irb/completion'
行雁书 2024-09-05 03:20:05

您可以将任何 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.

放低过去 2024-09-05 03:20:05

这些不是直接答案,但可以满足您更多了解 irb 的愿望。

控制台对象可以使用许多“irb”方法。

methods.grep(/irb/).sort
=> ["irb", "irb_bindings", "irb_cb", "irb_change_binding", "irb_change_workspace",
"irb_chws", "irb_context", "irb_current_working_binding", "irb_current_working_workspace",
"irb_cwb", "irb_cws", "irb_cwws", "irb_exit", "irb_fg", "irb_jobs", "irb_kill", "irb_load",
"irb_pop_binding", "irb_pop_workspace", "irb_popb", "irb_popws", "irb_print_working_binding",
"irb_print_working_workspace", "irb_push_binding", "irb_push_workspace", "irb_pushb",
"irb_pushws", "irb_pwb", "irb_pwws", "irb_quit", "irb_require", "irb_source", "irb_workspaces"]

玩得开心。

另一个是“conf”对象,它提供有关您的 irb 环境的反馈:

conf
=> conf.ap_name="irb"
conf.auto_indent_mode=false
conf.back_trace_limit=16
conf.debug_level=1
conf.echo=true
conf.ignore_eof=false
conf.ignore_sigint=true
conf.inspect_mode=nil
conf.io=#<IRB::StdioInputMethod:0x79da0>
conf.irb=#<IRB::Irb:0x7c58c>
conf.irb_name="irb"
conf.irb_path="(irb)"
conf.last_value=...
conf.line_no=6
conf.load_modules=[]
conf.prompt_c="%N(%m):%03n:%i* "
conf.prompt_i="%N(%m):%03n:%i> "
conf.prompt_mode=:DEFAULT
conf.prompt_s="%N(%m):%03n:%i%l "
conf.rc=true
conf.return_format="=> %s\n"
conf.thread=#<Thread:0x31790 run>
conf.use_readline=false
conf.verbose=nil
conf.workspace=#<IRB::WorkSpace:0x7aa84 @main=main, @binding=#<Binding:0x7a2a0>>

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.

methods.grep(/irb/).sort
=> ["irb", "irb_bindings", "irb_cb", "irb_change_binding", "irb_change_workspace",
"irb_chws", "irb_context", "irb_current_working_binding", "irb_current_working_workspace",
"irb_cwb", "irb_cws", "irb_cwws", "irb_exit", "irb_fg", "irb_jobs", "irb_kill", "irb_load",
"irb_pop_binding", "irb_pop_workspace", "irb_popb", "irb_popws", "irb_print_working_binding",
"irb_print_working_workspace", "irb_push_binding", "irb_push_workspace", "irb_pushb",
"irb_pushws", "irb_pwb", "irb_pwws", "irb_quit", "irb_require", "irb_source", "irb_workspaces"]

Have some fun playing around with those.

Another is the "conf" object that gives feedback about your irb environment:

conf
=> conf.ap_name="irb"
conf.auto_indent_mode=false
conf.back_trace_limit=16
conf.debug_level=1
conf.echo=true
conf.ignore_eof=false
conf.ignore_sigint=true
conf.inspect_mode=nil
conf.io=#<IRB::StdioInputMethod:0x79da0>
conf.irb=#<IRB::Irb:0x7c58c>
conf.irb_name="irb"
conf.irb_path="(irb)"
conf.last_value=...
conf.line_no=6
conf.load_modules=[]
conf.prompt_c="%N(%m):%03n:%i* "
conf.prompt_i="%N(%m):%03n:%i> "
conf.prompt_mode=:DEFAULT
conf.prompt_s="%N(%m):%03n:%i%l "
conf.rc=true
conf.return_format="=> %s\n"
conf.thread=#<Thread:0x31790 run>
conf.use_readline=false
conf.verbose=nil
conf.workspace=#<IRB::WorkSpace:0x7aa84 @main=main, @binding=#<Binding:0x7a2a0>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文