How to I start a rails console with pry turned off? - ruby-on-rails

Sometimes I have reason to want to start the rails console as an irb repl rather than pry (as awesome as pry is). It will default to pry because pry has in the Gemfile. Hows is that done nowadays?
I think there used to be a --irb option when running rails console but that seems to be gone now. I get a deprecation error message when I try it.
More details
If I just run "rails console" it takes me to pry.
If I run "rails console -irb=irb":
$ rails c -irb=irb
--irb option is no longer supported. Invoke `/your/choice/of/ruby script/rails console` instead
Relevent lines from my Gemfile:
gem 'rails', '3.2.18'
gem 'pry-rails'
gem 'pry-plus'

Launching pry when calling rails console or rails c is set up by the pry-rails gem. If you look in the pry-rails issues there is one that describes a solution.
Define the environment variable DISABLE_PRY_RAILS as 1.
So you can call rails console without pry with:
DISABLE_PRY_RAILS=1 rails c

Works in Rails 4: In your application.rb, inside your Application class, drop this puppy in.
# Use the IRB console instead of the Pry one
console do
require 'irb'
config.console = IRB
end
I couldn't take the Pry console anymore. It kept putting my cursor in odd places at unpredictable times. I can't even describe it but if you know what I'm talking about and know the solution, please let me know.

Inspired by the answers above, I added the following to the class definition in application.rb so that Pry is toggleable from the console:
console do
if ENV['IRB']
require 'irb'
config.console = IRB
end
end
You can then run rails c to get a Pry console, and IRB=true rails c to get an IRB console. This is easily modified if you want the inverse. Works in Rails 4 and 5.

For the benefit of anyone who runs into the same problem, this is my (crappy) workaround.
I wrapped the pry gems in Gemfile with this:
...
unless ENV['NOPRY']
gem 'pry-rails'
gem 'pry-plus'
end
...
Then run this from the unix terminal:
NOPRY=true bundle install
NOPRY=true rails console
Not pretty, but gets the job done...

You can also do it once console has already been started via IRB.start method.

Related

Pry not loading local app environment in Rails 4.2

I recently installed pry to replace my irb. It was working fine at first, but now every time I run pry it doesn't recognize the local app environment at all. I get something like the following:
[3] pry(main)> show-models
NameError: undefined local variable or method `show' for main:Object
from (pry):2:in `__pry__'
I've tried uninstalling and reinstalling the pry-rails gem and I've added the following code to an initializer file:
Rails.application.configure do
# Use Pry instead of IRB
silence_warnings do
begin
require 'pry'
IRB = Pry
rescue LoadError
end
end
end
Any thoughts on what this could be? I can't seem to find any info on this.
My gem file looks like this:
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'pry-rails'
gem 'pry-byebug'
end
You probably run Pry using pry command, when you should be using rails console command (be sure that you run it from your Rails app directory). If that doesn't work for you, then try bin/rails console and bundle exec rails console commands.
I also Use pry
I'm also using pry and not seen command like show-models yet. It doesn't work for me too. And I never use such command. Using Model.all working fine.I don't if commands you are using is feature of pry. If so visit its documentation. I think its problem due to you gave wrong command.
I had some different problems using pry and byebug together.
i deleted byebug and all is well.
Hope it will also solve ur problem.
I use gem 'pry' in my gemfile instead of gem 'pry-rails' though
Not sure what it was, but a system restart did the trick. Problem solved.

How to debug in rails server console?

I am new to ruby on rails, I want to debug my rails application through debugger i rails server console.So please tell me shortcuts and there meaning so that I can be able to debug.
In order to open the rails console in production, you can type:
RAILS_ENV=production bundle exec rails c
Or, if you want to debug code during runtime, you can use:
pry-remote
Rails 4 contains by default byebug and web-console. In development mode you can simply call byebug anywhere in the code to stop execution and get a debugger console.
In views you can use:
<%= console %>
to access an IRB console on exception pages.
See debugging guide.
If you don't have Rails 4, add to your Gemfile following:
group :development, :test do
gem 'byebug'
end
and run bundle install.
The official way to do this is using the byebug gem. Install it by executing
gem install byebug
After that you can put a byebug statement anywhere in your code:
class PeopleController < ApplicationController
def new
byebug
#person = Person.new
end
end
As soon as the application reaches this statement, it stops and presents a command prompt in the shell you started the server process.
See the RoR Debugging Guide for further details.

Pryrc does not seem to be loaded

I just started a new project with RAils 4.2 on ruby 2.1.2. In the Gemfile, it clearly states that pry-byebug is included, and in my ~/.pryrc file I have the following
puts "HELLO!!"
if defined?(Rails) && Rails.env
extend Rails::ConsoleMethods
end
require 'hirb'
Hirb.enable
When I do
$ rails c
I don't see "hellO' being printed and I don't get Hirb's pretty format
Any chance someone can tell me where should I start looking where the error might be?
Thank you!
I had to include gem 'pry-rails' in my Gemfile, and not within any groups.

Which gems do I need to debug a Rails application in RubyMine?

I use Ruby Mine 6.3, ruby 2.1 and Rails 4.0.3.
Which gems need to debug application with Ruby Mine, I tried use these gems:
gem 'debugger'
gem 'debugger-xml'
But application crashed with exit code 127 at breakpoint, and print this:
.rvm/gems/ruby-2.1.0/extensions/x86_64-linux/2.1.0/debugger-1.6.6/ruby_debug.so:
undefined symbol: rb_vm_get_sourceline
UPDATE
I updated RubyMine to 6.3.1, dropped 'debugger', 'debugger-xml' (in Gemfile only 'ruby-debug-ide') and Debugging works!
I advise bunch of "better_errors" and "binding_of_caller".
You can read about this here: https://github.com/charliesome/better_errors
I'm usually use this gems:
https://gist.github.com/MrEmelianenko/11078561
To enhance your console, use pry-rails, better with pry-doc.
To debug in program, like using breakpoint, step in/over, you can use pry-byebug or pry-debugger.
p.s. this is my way to debug rails app in terminal, I'm not sure if it is ok with Ruby Mine. Hope this can help.
I always liked pry. And its very useful with easy to learn screencast
This is my debugging stack for my Ruby 2 & Rails 3/4 apps ;)
group :development do
gem 'pry'
gem 'pry-nav'
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'pry-doc'
end
Hope it helps
As you mentioned, you need ruby-debug-ide, but not debugger or debugger-xml.
For Rubymine/IDea + Ruby plugin with Ruby 2.0/2.1, also try the debase gem along with ruby-debug-ide to speed up debugging. Earlier versions had some trouble, but it works well, as of 2014-04-21.
You already found your answer but I'd like to add small notes
Rubymine has an issue with the debugger gem, or probably it's the gems clashing(maybe), rubymine uses fast-debugger (ruby-debug-ide) and I think it doesn't like debugger, old versions suggested adding debugger-xml but from my trials that didn't really work so well, and current version kinda tolerates debugger a bit, but sometimes it freezes during breakpoints.
My teammates use debugger, so it's pushed to the repo, what I do is whenever I need to debug I just delete the debugger line from my Gemfile and wait for rubymine to detect that Gemfile change (about a second or two) and start my debugging session, I only put it back when I check the diff before pushing.

opening the pry console in sandbox mode

I recently found pry and I find it to be a great replacement for irb. I figured I'd use it as replacement for my ROR development and debugging.
I know that to open pry with a rails app you simply type
pry -r ./config/environment
My question is that is there a way to open the pry console in a sandbox mode so that any modification I make does not affect my database.
Firstly add "pry-rails" gem into Gemfile
gem 'pry-rails', :group => :development
Then bundle install
Then launch rails console in sandbox mode
# in development env
$ rails c --sandbox
# or in test env
$ rails c test --sandbox
That's all. Pry will replace irb automatially. Enjoy!
Ref: https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry#
If you don't want to modify your Gemfile, you can do this once you open pry:
require 'active_record/railties/console_sandbox'
I have this defined in a method in my ~/.pryrc so it's easy to use.

Resources