[英]How to wrap all methods in Rails console (IRB) in a block in Ruby?
I have seen http://ruby-doc.org/stdlib-2.0.0/libdoc/irb/rdoc/IRB.html and can see various ways to customize IRB. 我看过http://ruby-doc.org/stdlib-2.0.0/libdoc/irb/rdoc/IRB.html,并且可以看到各种自定义IRB的方法。 But did not see how to: 但是没有看到如何:
Write a block (or method) such that whenever a command is run in Rails console it runs the block. 编写一个块(或方法),以便每当在Rails控制台中运行命令时,它都会运行该块。
For example: 例如:
begin
puts :before
yield
puts :after
end
Then, when you do 然后,当你做
=> puts :OK
:before
:OK
:after
If you're able to switch from IRB to Pry (the pry-rails
gem will load it automatically for rails c
), it provides a hooks API that should do what you need. 如果您能够从IRB切换到Pry( pry-rails
gem会自动将其加载到rails c
),则它会提供一个可以满足您需要的钩子API 。 It sounds like you'd want the :before_eval
and :after_eval
events. 听起来好像您想要:before_eval
和:after_eval
事件。
I don't know of an equivalent mechanism for IRB, though it should be possible to hack something using TracePoint. 我不知道IRB的等效机制,尽管应该可以使用TracePoint破解某些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.