pry not available in the irb debugger - ruby

I want to use pry from within irb/debugger, so I can:
invoke step, next, continue, finish inside of pry
still be able to set breakpoints, etc.
What I did is the following:
$ gem install pry
$ gem install debugger
$ gem install debugger-pry
In the code I have inserted require 'debugger'; debugger
Then I start my program with ruby example, the irb promp starts and as described here it should display the pry command on help, but it does not.
ruby-debug help v1.5.0
...
(rdb:1) pry
*** Unknown command: "pry". Try "help".
Any idea how I could check whether it is installed correctly or what am I missing?

I think you need:
require 'debugger/pry'
you could add it to the top of your example.rb file.

Related

Can't get awesome_print gem to work

awesome_print looks like a pretty nice gem, so I wanted to try it out.
I went to one of my projects and did:
gem install awesome_print
and it says one gem installed, documentation installed, etc.
Then, while I am in that project, I went to my Rails console to try it out, but when I did a require "awesome_print" as their help file says, I get a "cannot load such file".
Has anyone got this to work?
gem install will put the gem code on your computer, but unless the gem's source code files are on your load path, require won't be able to find them. bundle exec looks at the nearest Gemfile.lock and adds the source code for all the gems listed there to your load path. Rails initialization includes getting Bundler to do this for you.
One solution is to add awesome_print to your Gemfile. However, this will cause your application to have awesome_print as a dependency. Alternatively you can manually add the awesome_print library to your load path after starting up the Rails console and then requiring it:
$ rails c
> $LOAD_PATH << path/to/awesome_print-x.x.x/lib
> require 'awesome_print'
> ap {foo: {bar: {baz: :qux}}}
If you're using RVM, the path is likely to be something like:
~/.rvm/rubies/ruby-x.x.x-pxxx#your_gemset_name/gems/awesome_print-x.x.x/lib
Add it to your Gemfile like this:
gem 'awesome_print', :require => 'ap'
I add it to the development group, since that's the only time I need it. The gem doesn't have any other gem dependencies, so I routinely add it to my Gemfile.
Also, add these two lines to your ~/.irbrc file to set ap to be your default pager:
require "awesome_print"
AwesomePrint.irb!
Note that if you use this, however, any projects where awesome_print is not installed in its Gemfile will raise this error when you run rails c:
cannot load such file -- awesome_print
Depending on whatever else you may have in your ~/.irbrc file, this can cause other side effects, such as messing up your prompt. To avoid these, simply add the two lines to the very end of that file.
install it :
$ gem install awesome_print
include it in you GemFile, if you want :
gem 'awesome_print', :require => 'ap'
add this line to the file ~/.irbrc :
require 'awesome_print'
AwesomePrint.irb!
restart your shell!
just a note: I did this and it didnt work right away, probably need to restart the computer... or I just needed to close all shell tabs and open the terminal again!
Install the gem on your machine
gem install awesome_print
Get the path to which it has installed
gem which awesome_print
Add the following configuration to your ~/.irbrc and ~/.pryrc. This will load Awesome Print whenever you fire an IRB or a pry session.
*Remember $LOAD_PATH will hold whatever you got from typing gem which awesome_print
# ~/.irbc and ~/.pryrc
$LOAD_PATH << "~/.asdf/installs/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/awesome_print-1.8.0/lib/"
require "awesome_print"
AwesomePrint.irb!
If you are looking to install it without having it in your Gemfile, this is how to do it:
$ gem install awesome_print
I was running into an issue where it was installing successfully but it not in the right directory.
In that case just put this in your .bashrc, this will set the load path:
export PATH="/home/user/.gem/ruby/2.3.0/bin:$PATH"
PATH="`ruby -e 'puts Gem.user_dir'`/bin:$PATH"
replace 2.3.0 with the version of ruby you are working with.
replace user with your username or if you are using vagrant then replace with vagrant
reload your .bashrc or exit the Terminal to reload changes, then install the gem again.
In my case, I struggled with PATHs and such, while missing something obvious!
# which ruby
/usr/bin/ruby
# ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]
# locate bin/ruby
/usr/bin/ruby
/usr/local/Cellar/ruby/2.7.2/bin/ruby
/usr/local/opt/ruby/bin/ruby
# /usr/local/opt/ruby/bin/ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin17]
#
Aha! Version crud. I was running an old ruby. Thanks, Apple!
# sudo mv /usr/bin/ruby /usr/bin/ruby_2.3.7
# sudo ln /usr/local/opt/ruby/bin/ruby /usr/bin/ruby
Solved the problem!
There is probably something I could have told brew to do to fix things, but I was impatient. :-)

Trying Ruby: why can't I install nanoc?

I try to install nanoc http://nanoc.stoneship.org/docs/2-installation/
by typing in irb
gem install nano
it says undefined variable or method 'nanoc' ?
You need to install it from the shell, not IRB. Gem is a command, i.e.
% which gem
/usr/bin/gem
% gem install nanoc
ian.
That gem install command is meant to be run in your normal system shell (something like Bash, for example).
irb is a Ruby shell, it interactively executes Ruby code. You'll notice that the instructions you link to immediately tell you to quit irb after starting it (they only told you to run it to make sure Ruby was installed).

I see gem in "gem list" but have "no such file to load"

I am on Ubuntu10
sudo apt-get install ruby1.9.1-full
then download sources of rubygem 1.3.7 and install it
sudo ruby setup.rb
then, for example, install sinatra
sudo gem install sinatra
Finally open irb and type
require "rubygems"
require "sinatra"
and get error
LoadError: no such file to load -- sinatra
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
I had exactly this problem. The problem is that gem and ruby disagree about where the gems live. Compare these:
ruby -e "puts Gem.path"
gem env
gem which sinatra
If you're like my setup, you'll notice that there's an entry in gem env's paths that isn't in Gem.path, and that's exactly where sinatra will claim to be. In my case, I had to add
export GEM_HOME=/usr/lib/ruby/gems/1.9.1
to my .profile. Then everyone was happy.
Execute
sudo gem install sinatra --verbose
and note the path where the gem is getting installed.
Then try this in irb
puts $LOAD_PATH
and make sure that gem is installed in one of the directories in $LOAD_PATH
And ideally just start using http://rvm.beginrescueend.com/
I usually hit this error when I forget:
require 'rubygems'
It'd be helpful if you provided the actual code sample, though, what gem you want to require, and what Ruby version you're using if this doesn't solve the problem.
This was before here on SO quite a few times. Problem is that you probably have two versions of ruby. The one is installing the gem and the other one is trying to use it. Do this in terminal:
$ which -a ruby
Or this:
$ which -a gem
to see if you have more than one version of ruby/gem installed. If so - remove one version (via $ rm or package manager of your system).
I use ruby gems 1.8.7 for a project. I was getting the same error. Use the line require 'rubygems'. It must always be the first require statement, otherwise you can get an error. In my code, I had
require 'watir'
require 'rubygems'
# more code
I got the error - in `require': no such file to load -- watir (LoadError).
When I put rubygems first, the error went away and everything worked. I don't know
why this happens.
Btw, I tried user24359 answer and it did not help me.
C:\code>ruby -e "puts Gem.path"
-e:1: uninitialized constant Gem (NameError)

What do I put in my Ruby (JRuby) code to break into the debugger?

I'm familiarizing myself with some JRuby code, and I'd like to be able to place a breakpoint in the code and run (as usual) from the command-line, having it break into the debugger when it gets to that point. Is there something I can put in my code to force JRuby to break into the debugger?
I've tried running jruby -r debug foo.rb (instead of the usual jruby foo.rb), and then setting a breakpoint with b bar.py:98, and then continuing. But the debugger stops every time there's an exception, and there seem to be a lot of them before it gets to the line of code I'm interested in. I'd like to be able to put the "break-into-debugger" line(s) in my code and run jruby foo.rb and have the first place the debugger stops be at that line.
(i.e. I'm looking for the Ruby/JRuby equivalent of import pdb;pdb.set_trace() in Python.)
You could try Netbeans Ruby IDE, it has JRuby interpreter and debugging tools embedded and you can debug visually in the IDE directly.
If using an IDE is not an option for you, just install de debug gem into your JRuby distro and use it via debugger command:
Manually download the ruby-debug-base-0.10.3.1-java.gem from debug-commons to a local directory.
Install the Gem into your JRuby Gem repository:
jruby -S gem install -l ruby-debug-base-0.10.3.1-java.gem
Install ruby-debug gem:
jruby -S gem install --ignore-dependencies ruby-debug
The debugger command should work now.
# test.rb
require 'rubygems'
require 'ruby-debug'
debugger
# run like this:
jruby --debug -S rdebug test.rb
More information on Netbeans wiki, rdebug wiki and JRuby wiki
require 'rubygems'
require 'ruby-debug'
debugger
I am using JRuby 1.7.3 (1.9.3p385) on Windows 7.
Contrary to what people say, there does not seem to be a version of ruby-debug preinstalled with JRuby. However, I installed it by running gem install ruby-debug. I start the debugger in my code like this:
require 'rubygems'
require 'ruby-debug'
debugger
You should call the debugger method everywhere that you want to have a breakpoint.
I ran the code like this:
jruby --debug program.rb
The --debug option is optional, but without it you get a warning that "tracing (e.g. set_trace_func) will not capture all events without --debug flag".
Another answer on here used the -S rdebug option on the command line. That option is not necessary for debugging and it makes the debugger start at the very beginning of your program. Just call the debugger method to start the debugger when you need it; don't use that option.
Even without the ruby-debug gem, it seems like there is still some kind of basic debugging built in to JRuby via debug.rb. You can simply write load 'debug.rb' in a file to start a debugger going. It prints out some junky messages ("Debug.rb" and "Emacs support available.") but it seems to work.

How to set wirble colorize on XP

I'm having problems setting up wirble on XP machine. when i run IRB i get sth like this:
"foo".capitalize
=> ←[0;31m"←[0;0m←[0;36mFoo←[0;0m←[0;31m"←[0;0m
this occurs only if I call colorize on wirble either in .irbrc or after starting irb. I installed ruby 1.8.6 with the all-in-one installer. Any ideas on how to get colors working?
First install these two gems:
gem install win32console
gem install wirble
Then in your irb console do:
require 'win32console'
include Win32::Console::ANSI
require 'wirble'
Wirble.init
Wirble.colorize
Your input will look corectly:
"foo".capitalize
=> "Foo"
(Foo is in blue on my screen now)
P.S.
If you want irb always to start with these settings just put the above code in the '.irbrc' file in your home directory.
Try this:
http://www.botvector.net/2008/06/colorized-wirble-in-windows-xp.html
(which was the first Google hit for "wirble windows")

Resources