pry-rails
pry-rails copied to clipboard
Console hooks are broken
Hi,
I'm trying to add default methods to the console like this:
# config/application.rb
# Source http://opensoul.org/2012/11/08/add-helper-methods-to-your-rails-console/
console do
require 'console_foo'
Rails::ConsoleMethods.include(ConsoleFoo)
end
Rails includes Rails::ConsoleMethods
into the console::ExtendCommandBundle
module, in this case that'd be Pry::ExtendCommandBundle
which is an empty module so nothing is happening.
I could get this working by doing TOPLEVEL_BINDING.eval('self').extend ConsoleFoo
but that seems a lot like a hack, is there a way we could have ExtendCommandBundle
work?
The console
block gets called before console.start
(Pry.start
) so maybe there's something to be done there?
Thanks
Just ran into this as well. Can confirm that switching back to IRB mitigates the problem.
When you run include Rails::ConsoleMethods
in the console, the methods are available again.
class Application < Rails::Application
module ConsoleHelpers
def bar; :bar; end
end
console do
module Rails::ConsoleMethods
include ConsoleHelpers
def foo; :foo; end
end
end
end
rails c
Loading development environment (Rails 4.1.6)
[1] pry(main)> foo
=> :foo
[2] pry(main)> bar
NameError: undefined local variable or method `bar' for main:Object
from (pry):2:in `__pry__'
Funny right?
@mlangenberg yes. Going back to plain IRB fixes the problem. I actually ended up extending TOPLEVEL_BINDING
. I blogged about this here.
Thanks for documenting your workaround @nhocki :+1: