How to require pry during development of a ruby gem? - ruby

I'm trying to develop a ruby gem for practice and I'm wondering how do I require pry during development and test runs? Is there anyway to require the gem only during development? I'm on Ruby and not Rails and I don't think I have any environment variables setup to rely on. Is there a conventional way to do this?
and
Currently if I run code that hits the above line, I get this error:
NoMethodError: undefined method `pry' for #<Binding:0x007f8d3287c4d8>
from /Users/jwan/programming/interview_questions/gemini/jobcoin_client/lib/jobcoin_client/requests/connection.rb:18:in `post'
A few questions:
How do I properly require pry so this line doesn't throw an error when developing a gem?
I read Yahuda's post but I'm still unclear why adding dependencies in the gemspec vs adding dependencies in the Gemfile. What is the difference?
Currently, after I make changes to the ruby gem, I have to run these series of commands. Is there anything more efficient that I can do?
gem build jobcoin_client.gemspec
WARNING: no homepage specified
WARNING: open-ended dependency on pry (>= 0, development) is not recommended
if pry is semantically versioned, use:
add_development_dependency 'pry', '~> 0'
WARNING: See http://guides.rubygems.org/specification-reference/ for help
Successfully built RubyGem
Name: jobcoin_client
Version: 0.1.0
File: jobcoin_client-0.1.0.gem
$ gem install jobcoin_client
Successfully installed jobcoin_client-0.1.0
Parsing documentation for jobcoin_client-0.1.0
Done installing documentation for jobcoin_client after 0 seconds
1 gem installed
05:45 PM
$ irb
irb(main):001:0> require 'jobcoin_client'
=> true
irb(main):002:0> require 'pry'
=> false

You can install it to the system and use a boolean flag to conditionally require it and set breakpoints.
gem install pry
Then in code, something like this:
SET_BREAKPOINTS = ENV["SET_BREAKPOINTS"] == "true"
require 'pry' if SET_BREAKPOINTS
binding.pry if SET_BREAKPINTS
To turn breakpoints on, you can manipulate env through code:
ENV["SET_BREAKPOINTS"] = "true"
or when calling a script from bash:
env SET_BREAKPOINTS=true irb -r 'your_gem'

Related

Is the net/http gem not in the default library for Ruby?

I want to create a Gemfile.lock by typing 'bundle install' but my local machine can't find the gem net/http. I've tried typing 'bundle update net/http' & 'bundle --full-index' & 'gem install bundler' but I keep getting this error when I try 'bundle install' again:
Could not find gem 'net/http' in rubygems repository https://rubygems.org/ or installed locally. The source does not contain any versions of 'net/http'
my Gemfile resembles the following:
source "https://rubygems.org"
gem 'open-uri'
gem 'nokogiri'
gem 'net/http'
gem 'pry'
Other solutions to this problem suggest removing the line for gem net/http because net/http is part of the default library for Ruby...however when I do this everything loads fine, and I can create a Gemfile.lock upon typing 'bundle install' but when I run my code I get the following error:
Traceback (most recent call last):
run.rb:4:in `': uninitialized constant Net (NameError)
Did you mean? Set
The line of code this refers to is
response = Net::HTTP.get(url)
I'm running Ruby version 2.6.1
The name of the gem is net-http. It is one of the first hits when you google for "net/http".
However, in Ruby 2.6.1, net/http is still part of the standard library, not a gem. Net/http was only removed from the standard library in Ruby 3.0.
There are two different kinds of standard gems:
Default gems: These gems are part of Ruby and you can always require them directly. You cannot remove them. They are maintained by Ruby core.
Bundled gems: The behavior of bundled gems is similar to normal gems, but they get automatically installed when you install Ruby. They can be uninstalled and they are maintained outside of Ruby core.
Your problem is the wrong name gem ('net-http' instead of 'net/http', you can run gem search ^net to find out remote gems start by 'net').
If the gem is 'default', no need to declare it in Gemfile.
You can check standard default gems on: https://stdgems.org/

Ruby not finding gems, but they appear on gem list, and work in irb console?

I've got a Ruby app using deamon-kit to create a daemon that runs a cron task every 3 seconds.
Problem is I'm trying to add some error checking using Errbit, so that requires me to:
require 'hoptoad_notifier'
in my script. However, the script is complaining it can't find the file?
.rvm/gems/ruby-1.9.2-p320#stitch/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require': no such file to load -- hoptoad_notifier (LoadError)
What confuses me is that the gem is installed, when I run
gem list | grep hoptoad_notifier
I get
hoptoad_notifier (2.4.11)
Another test I did was to pop into irb console, on the same terminal window, after making sure I'm inside the correct RVM gemset off course:
1.9.2p320 :001 > require 'hoptoad_notifier'
=> true
1.9.2p320 :002 >
And voila, hoptoad is loading. It's only when loading my deamon-kit deamon, that I get the error.
What further confuses me is that when I look at my require block:
require 'rubygems'
require 'resque'
require 'hoptoad_notifier'
It's finding rubygems and resque, but not hoptoad_notifier? Why, when I comment out hoptoad, it doesn't also complain about resque and rubygems?
dameon-kit uses bundler, so you don't need to include rubygems. Include the following lines to your Gemfile :
gem 'resque'
gem 'hoptoad_notifier'
Run bundle install
and include your gems as usual :
require 'resque'
require 'hoptoad_notifier'
It worked for me.

How to install Ruby gem automatically, if required?

Given a Ruby program that uses a particular gem (e.g. term-ansicolor), how could I install the gem automatically, if required?
In other words, the program does:
require 'term/ansicolor'
and, in case the gem wasn't install previously, I would like to install it and continue the program rather than getting an error:
LoadError: no such file to load -- term/ansicolor
from (irb):1:in `require'
from (irb):1
from :0
What would be the most appropriate method to achieve that?
I've been using this pattern:
require 'rubygems'
begin
gem 'minitest'
rescue Gem::LoadError
Gem.install('minitest')
gem 'minitest'
end
require 'minitest'
Such a tool is not available. Read a detailed discussion about it here http://www.ruby-forum.com/topic/2728297
You should consider to use bundler. It's the de-facto standard way to manage dependencies in Ruby software.
Alternatively, you could build your package into a gem and put the required gem as a dependency in the gemspec. It would then get installed automatically when you install the gem.

How to color unit tests with lib minitest or Test:Unit?

I would like to have unit tests output color in my dev environment. However, I can't make it work on Linux (Debian and Ubuntu). When I include the following libs:
require 'minitest/autorun'
require 'minitest/unit'
require 'minitest/pride'
I get:
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/minitest-2.3.1/lib/minitest/pride.rb:35:in `<top (required)>': undefined method `output' for MiniTest::Unit:Class (NoMethodError)
caused by the code:
MiniTest::Unit.output = PrideIO.new(MiniTest::Unit.output)
I have seen a working Rspec variant. Unfortunately, my Ruby knowledge is not enough to see differences.
Give turn a whirl.
Add this to your Gemfile:
group :test do
gem 'turn', :require => false
end
step 1 : use the latest version of the gem (I think it will be fixed in Ruby 1.9.3)
gem install minitest
step 2 : require "minitest/pride" on the command line, not in your code
ruby -rminitest/pride your_ruby_script.rb
.. and in your code simply require 'minitest/autorun'
require 'minitest/autorun'
If you use Rubymine, just add
-rminitest
in the default configuration of the tests.
=> the configuration would like
-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -rminitest/pride
simply add these lines to your test_helper.rb file after require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
in your gemfile add
gem 'minitest-reporters', '~> 1.0.7'
this will get your rake test to be in red and green, but it also brings up the backtrace. to get rid of all those extra backtrace logs add this to your gemfile then bundle:
gem 'mini_backtrace'
then in config/initializers/backtrace_silencers.rb add this line to cut all the extra rvm stuff
Rails.backtrace_cleaner.add_silencer { |line| line =~ /rvm/ }
hope that works for you, let me know if you need more details.
See https://github.com/tenderlove/purdytest/issues/1. It seems to be a known bug with the minitest version shipped with 1.9.2. For the others, adding
gem "minitest"
at the very top of your file did the trick.
I currently use purdytest with 1.9.2
Try look at this post about using turn gem for nice looking and configurable output for minitest.
http://rawonrails.blogspot.com/2012/01/better-minitest-output-with-turn-gem.html
$ gem install mynyml-redgreen --source http://gemcutter.org
# in your test file
require 'redgreen'
redgreen and turn work nicely in conjunction with each other, by the way

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)

Resources