pry-rails icon indicating copy to clipboard operation
pry-rails copied to clipboard

Console hooks are broken

Open nhocki opened this issue 9 years ago • 3 comments

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

nhocki avatar Jun 03 '15 23:06 nhocki

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 avatar Jul 31 '15 20:07 mlangenberg

@mlangenberg yes. Going back to plain IRB fixes the problem. I actually ended up extending TOPLEVEL_BINDING. I blogged about this here.

nhocki avatar Aug 01 '15 04:08 nhocki

Thanks for documenting your workaround @nhocki :+1:

mlangenberg avatar Aug 04 '15 14:08 mlangenberg