How to add "pry" when developing a Ruby gem - ruby

I have a gem called "something".
I would like to add pry as a development dependency when developing the gem. However I don't know how to load it.
If I have "require something" inside lib/something.rb , when I release the gem, it throws a LoadError, because pry is only a development dependency.
At the same time I don't want to keep adding and removing pry when I am committing code.
What is the best way to require pry only when developing the application, but not require it as a dependency for the gem?

You can use the add_development_dependency in the gemspec file. You'll still have to require it in your lib/something.rb file within a begin .. rescue LoadError block. (Edit 2, see below)
In your case, it will be something like the following:
spec.add_development_dependency 'pry', '~> 0.9.12.2'
The purpose of add_development_dependency is to separate the gems into dependencies that get installed when you execute gem install mygem vs development-only dependencies that are installed only when you execute gem install mygem --development.
Edit: #Pierre-Louis Gottfrois' solution
Modify the Gemfile directly and add a test group. This question describes the process. This does not appear to be a preferred solution according to Yehuda Katz.
Edit 2: begin require ... rescue LoadError is apparently a common practice for Ruby scripts, according to this Making Ruby Gems article.

I think I found a workaround for that.
If you configure bundler to use pry as your console with
$ bundle config console pry
Then pry is itself required and you don't need to explicitly require in your source files.
Plus, you get a history on pressing ' ↑ '.

Related

When is the 'require' necessary when using a ruby gem?

I noticed for some gems you must include it in the file where you want to use it like this require 'a_gem', but this is not always the case.
I am going to compose a gem by myself. What should I do if I do not want to add the require 'my_gem' to the .rb file when using it?
Usually, an application that is using a gem needs to require the gem:
require "my_awesome_gem"
MyAwesomeGem.do_something_great
However, if an application is using bundler, which defines the application's gem in a file called "Gemfile":
source 'http://rubygems.org'
gem 'my_awesome_gem'
then the application may invoke bundler in a way that automatically requires all of the gems specified in the Gemfile:
require "bundler"
Bundler.require
MyAwesomeGem.do_something_great
Rails projects use Bundler.require, so a Rails application will not need to explicitly require a gem in order to use it: Just add the gem to the Gemfile and go.
For more about Bundler.require, see the bundler documentation
For more about how Rails uses Bundler, see How Does Rails Handle Gems? by Justin Weiss.
This doesn't make sense. If you want to write a Gem and use it, it needs to be required somewhere.
This "somewhere" could be explicit in one of your scripts, it could be written in a Gemfile or it could be required by another script/gem that is required by your script.
If you write a gem, Ruby will not include it automatically in all your scripts.
Finally, if you publish it, should every single Ruby script on earth magically require it and execute your code?
I suppose that the project you have seen which didn't use require 'a_gem' was using a Gemfile.

How to include Ruby Gems on Github

So, I've created a GitHub to manage my latest Ruby project, and I want for it to utilize a couple of gems. On my PC, all I have to go is type
gem install "gemName"
and it loads it to my computer, and then all I have to do in my Ruby script is have
require "rubygems"
require "gemName"
How can I do this with GitHub? What I tried to do is create a subfolder from the main repository (called "RubyGems") and then in my main ruby script
require "/RubyGems/colorize"
require "/Rubygems/psych"
With the two gems (colorize and psych) in the "RubyGems" folder.
Is this the proper way to do this? Will this even work? What is the right way to do this? (Sorry, I'm kinda new to GitHub.)
A couple of things, unless you're using a really old version of Ruby (like 1.9) you don't need to require 'rubygems' because is already required by default, next I highly recommend you to get familiar with bundler.
Bundler is used for "bundling" the required gems you use, to so do you have to install the gem (gem install bundler) and then you create a Gemfile, like this:
source 'https://rubygems.org'
ruby '2.2.0'
gem 'colorize', git: 'https://github.com/fazibear/colorize.git'
gem 'psych'
Execute bundle install after, that will create Gemfile.lock file, make sure you push both files to your repository.
With that you would be able to bundle exec ./your-script.rb, assuming your script is something like this:
require 'psych'
require 'colorize'
# Here I do stuff with psych and colorize

Pry is not a module

Ok, as #tim-moore asked, I will post it in new question.
Ok, so I wanted to make gem using bundle. Pry extension gem require that gem start with pry- as mentioned here.
I used:
bundle gem pry-name
but it messed up my file structure
create pry-name/pry-name.gemspec
create pry-name/lib/pry/name.rb
create pry-name/lib/pry/name/version.rb
As you can see it created lib/pry directory. I know it's gem's style to created such structure but now I pry cannot load this gem automatically
One solution from my question was:
create pry-name.rb that contain only require 'pry/name'
After I have done this, and build gem, I started pry:
This message appear:
require 'pry-name' # Failed, saying: Pry is not a module
As for my guesses:
I'm creating commands writing something like this:
Pry::Commands.create_command "name-of-command" do
# my code goes here
end
and, as ruby find Pry::Commands. it want require it from lib directory not from Pry gem.
What does this error mean. Why it doesn't work. How make it work keeping in mind gem and pry requirements(pry gem starts with pry- and gem will create another directory(ies) when someone use - for example: gem pry-name will make pry/name)
Everywhere in your newly-created gem where it has module Pry, change it to: class Pry. Since Pry is already defined (as a class), you cannot redefine/reopen it as a module.

Is it possible to require files outside the Gemfile?

For example, I'm developing a gem, and while I'm developing, I use pry instead of IRB, and debugger for debugging. However, I don't want possible contributors to have to install them (because they may not need them). My first idea was to put them in a Bundler group:
source :rubygems
gemspec
group :extras do
gem "pry"
gem "debugger"
end
And then people could use:
$ bundle install --without extras
But I want it to be a default that they're not installed. What would be perfect is that they're not in my Gemfile, but that I can still require them (if they exist on the computer). This solution would be ok because I don't care at which version they're locked. Can it be done?
You can add arbitrary load paths and then require gems from them. Check out the global variable $:
puts $:.inspect
# ["/var/myproject/releases/20200918191637/lib", "/var/myproject/releases/20200918191637/vendor", "/var/myproject/releases/20200918191637/app/assets", "/var/myproject/releases/20200918191637/app/controllers", "/var/myproject/releases/20200918191637/app/helpers", "/var/myproject/releases/20200918191637/app/mailers", "/var/myproject/releases/20200918191637/app/models", "/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-2.0.2/lib", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/json-1.8.3/lib", "/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-2.0.2/lib/gems/bundler-2.0.2/lib", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/x86_64-linux", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0/x86_64-linux", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-linux"]
Now let's append to $: and require a gem
require 'method_source'
# LoadError: cannot load such file -- method_source
$: << '/home/deploy/.rvm/gems/ruby-2.2.4#myset/gems/method_source-1.0.0/lib'
# ["/var/myproject/releases/20200918191637/lib", "/var/myproject/releases/20200918191637/vendor", "/var/myproject/releases/20200918191637/app/assets", "/var/myproject/releases/20200918191637/app/controllers", "/var/myproject/releases/20200918191637/app/helpers", "/var/myproject/releases/20200918191637/app/mailers", "/var/myproject/releases/20200918191637/app/models", "/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-2.0.2/lib", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/json-1.8.3/lib", "/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-2.0.2/lib/gems/bundler-2.0.2/lib", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/x86_64-linux", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0/x86_64-linux", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-linux", "/home/deploy/.rvm/gems/ruby-2.2.4#myset/gems/method_source-1.0.0/lib"]
require 'method_source'
# true
That can be tedious if you have a lot of dependencies, though, so you might try this:
Dir['/home/deploy/.rvm/gems/ruby-2.2.4#myset/gems/*'].each do |gem_path|
$: << File.join(gem_path, 'lib')
end
bundle install is "opt-out"—unless you specify --without some_group, it installs everything.
If you absolutely don't want to have a given gem in your Gemfile, you could just gem install that rogue gem outside of your bundle. Then it'll be visible to you under irb and straight ruby (but obviously you'll get errors if you try to require it within code running under bundle exec).
You could add a conditional based on environment variables into the Gemfile. Example:
source :rubygems
gemspec
if ENV['WITH_EXTRAS'] == '1'
gem "pry"
gem "debugger"
end
The gems are then only installed/loaded, if you set the environment variable to '1' e.g. WITH_EXTRAS=1 bundle install.
Nowadays you can use the "optional" argument, and it will do exactly what you ask: it will not install the extras unless people bundle --with extras
group :extras, optional:true do
...
end
https://bundler.io/v2.3/guides/groups.html#optional-groups

After installing a gem within a script, how do I load the gem?

I have a small Ruby script that I'm writing to automate the preparation of a development environment on local machines. Because I can't be certain that the rubyzip2 library is present on all of the machines, I'm having the script install it when needed.
Currently, my script is doing the following:
begin
require 'zip/zip'
rescue LoadError
system("gem install rubyzip2")
end
Once the gem has been installed, the script continues execution; however, the gem hasn't been loaded so all code requiring rubyzip2 halts the execution.
How do I load the gem into memory so that the script can continue running after installation?
Instead of doing require 'thegem' and rescuing error, you should check the gem availability before, and then, if needed, install it. After, you can require it.
Take a look at this post for the gem availability
Or this post
EDIT
After installation, you need to clear gem paths if you don't want to reload your script.
You could achieve this with this method :
Gem.clear_paths
There are already answered questions here
So your code should looks like this ( for example ) :
begin
gem "rubyzip2"
rescue LoadError
system("gem install rubyzip2")
Gem.clear_paths
end
require 'zip/zip'
With bundler version higher than 1.10 (to update just run gem install bundler) you can use its new 'inline' feature as described here.
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'catpix'
end
puts Catpix::VERSION
First parameter of gemfile method is whether gems that aren't already installed on the user's system should be installed.
Use Bundler with your application/library. This will allow users to run bundle and all the gems will be fetched and ready for you to use.
Ok so you may want to use Bundler and set up a Gemfile then have bundler do a bundle install, bundler will fetch out all the gems and install them if it is not already installed and you can then require all the gems in the gem file. Read the documentation in the link for more information.
But what you are looking to do specifically in your question is to use the retry keyword. This keyword will retry the loop after the rescue was called.
So if you require the gem and it fails and the Load Error Exception is called. The Begin Block will rescue, the system call will install the gem, then it will retry and require the gem. Just cautious because this may lead to an infinite loop unless you want to set up a condition to maybe retry it only once.
begin
require 'zip/zip'
rescue LoadError
system("gem install rubyzip2")
retry
end

Resources