Ruby - Cannot use locally installed gem - ruby

I've written a simple PasswordGenerator gem that I have at ~/workspace/gems/password_generator and have an app at ~/workspace/rubysamples/app where I want to use it. I have a Gemfile, the content of it is this:
gem 'password_generator', path: '~/workspace/gems/password_generator'
I installed it locally, like this:
bundle install --local
Resolving dependencies...
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `~/workspace/gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
It looks like it's installed locally:
bundle info password_generator
* password_generator (0.1.0)
Summary: Simple password generator
Homepage: https://github.com/jedrekdomanski/password_generator
Path: /home/jedrek/workspace/gems/password_generator
When I try to use it
~/workspace/rubysamples/app/password_reset.rb
PasswordGenerator.generate
I get an error
uninitialized constant PasswordGenerator (NameError)
What am I doing wrong? Am I missing anything?
Here's my gem repo: https://github.com/jedrekdomanski/password_generator
I also tried pointing to my repo and branch in the Gemfile
gem 'password_generator', git: 'git#github.com:jedrekdomanski/password_generator.git', branch: 'master'
but I get the same error message uninitialized constant PasswordGenerator (NameError)

There are potentially two issues. The first is how you are starting Ruby and the second is how you are requiring your module.
First, if you are starting Ruby by running ruby password_reset.rb then you are ignoring the Gemfile. The Gemfile is only used when you're using bundler, so you want to make sure you are starting Ruby by running bundle exec ruby password_reset.rb. This causes bundler to read your Gemfile and execute Ruby in that context.
Second, you're not properly including your module in your Ruby file. Just because you've added the gem to your Gemfile and started Ruby using bundler doesn't mean that the Ruby process knows you intend to use that gem's module; it just makes the module available for use. You might wonder, "Why don't I have to do that in Rails?" Because Rails does that for you automatically via config/application.rb.
Given these two issues, the correct way to accomplish your goal is to configure your app as follows:
First, create your Gemfile:
# Gemfile
gem 'password_generator', path: '~/workspace/gems/password_generator'
Second, create your password_reset.rb file:
# password_reset.rb
# Manually require any libraries that this app will use, even if defined in Gemfile
require 'password_generator'
# Call `puts` so something is printed to the console when this app runs
puts PasswordGenerator.generate
Third, run bundle install to ensure your Gemfile is properly formatted and to generate your Gemfile.lock:
⇒ bundle install
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `../../gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Fourth, run bundle exec ruby password_reset.rb and observe the output:
⇒ bundle exec ruby password_reset.rb
kpiDfyTxtdAsKmYuZqmK
Everything works because:
Ruby is started with Bundler
Bundler reads your Gemfile and makes the gems available to Ruby
Your app requires the module from the gem before attempting to use the module

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/

Make ruby script use local gems, instead of common

I'm deploying my rails project to production server. There is only 1.9.3 version of ruby (I developed on 2.1.2) so there is few compatibility problems in gems versions. More over, I downloaded one of gems to vendor/gem_name and made necessary fixes in its sources, so I need to use exactly my version of that gem and, as you understand, It's not possible to update it.
in Gemfile
require 'gem_name', :path => 'vendor/gem_name'
So after cloning project to server I run
bundle install --path vendor/bundle
and it created bundle directory in vendor folder with gems versions, needed to me, inside it.
After that I tried to run fetching script to fill db with some data by command
ruby *_fetch.rb
inside *_fetch.rb:
require 'gem_name'
And it fails with error
You have already activated gem_name older_version, but your Gemfile requires
gem_name newest_version. Using bundle exec may solve this. (Gem::LoadError)
from /usr/local/rvm/gems/ruby-1.9.3-p374/gems/bundler-1.3.5/lib/bundler/runtime.rb:19:in `setup'
So how could I specify script to require my edited local gem?
Run it with bundle exec That's exactly what bundle exec is for.

Bundler does not install from stash private repo, but reports that it does

My bundle file doesn't appear to be pulling down a gem from a private repository properly.
Inside of my Gemfile, I have:
group :internal do
gem 'private', git: 'ssh://git#internalserver.org:<port>/gems/private.git'
end
This runs, and verbose logging produces:
Updating ssh://git#internalserver.org:<port>/gems/private.git
Cloning into '/Users/<username>/.rvm/gems/ruby-2.0.0-p247/bundler/gems/private-ddec73caf50f'...
done.
When I navigate to /Users/<username>/.rvm/gems/ruby-2.0.0-p247/bundler/gems/, I see the correct repository cloned properly, with a gemspec with the correct name.
When bundler is finished running, gem list does not show the private gem. It produces an error when I attempt to require it.
I tried deleting the Gemfile.lock file in the repository and rerunning, and that did not work. All of the public gems in the Gemfile install correctly.
Relevant version numbers / software:
Bundler version 1.3.5
rvm 1.23.14
ruby 2.0.0p247
Atlassan Stash
Git gems are a Bundler-specific extension to Rubygems. The gem command does not know about these, so they're not listed by gem list. You can run bundle show to see the list of gems that are recognised by Bundler, which will include git gems.
To require the gem, you'll need to be sure that the load path is set up correctly by Bundler. There are three ways to do this:
Call require 'bundler/setup' in your app. This is typical for Rails apps. More on Bundler.setup
Call bundle exec <command> to run the command. This is more common when running commands from a gem, such as rake or rspec. More on bundle exec
Create binstubs for commands that you run frequently.
See http://bundler.io/v1.5/git.html for more information on git gems.

Create gem locally and skip rake install

I'm creating a ruby gem using Bundler for a simple rackup app (not Rails). It's a real pain to run rake install and then restart the webserver everytime. For most part it's ok because I test everything using rspec but not design. My gem contains a whole lot of design and everytime I update my gem I have to go threw the same procedure.
Is it possible to build gems locally without having to run rake install and then restart my rack server every single time?
If you're using Bundler to manage the gems in your application, you can use Bundler's path directive to use a gem that's currently in development.
In your Gemfile:
# My awesome gem that I'm developing
gem 'some-awesome-gem', :path => '~/Projects/some_awesome_gem'
Essentially, just point the path at the directory where your gem resides and you won't have to package new versions of the gem while you're actively developing it.
See the Bundler homepage and Gemfile Manual for more details.

Bundle install runs from incorrect directory

I'm building a simple thor based generator for some internal projects, and can't seem to get bundle install running from the correct directory.
As I run the new [APP_NAME] function, it should create the directories and files, then run bundle install to install the gems required for the application.
The source of the generator function:
def create
puts "Creating application #{name}"
directory 'application', "#{name}"
Dir.chdir("#{Dir.pwd}/#{name}") do
puts `bundle install`
end
end
And the console output from running the command that calls this create method:
$ bundle exec bin/my_gem new test_app
Creating application test_app
create test_app
create test_app/Gemfile
create test_app/Guardfile
create test_app/README.md
create test_app/app/controllers
create test_app/app/helpers
create test_app/app/models
create test_app/app/views
Using thor (0.14.6)
Using my_gem (0.0.1)
Using bundler (1.1.3)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
As you can see, it is running bundle install but it's running it in my current directory (thor, bundler, my_gem), as opposed to the test_app directory (guard, guard-coffeescript, guard-less, and others) .
Running other commands such as ls or pwd give the expected results:
Gemfile
Guardfile
README.md
app
and
/Users/davidlumley/Development/Gems/my_gem/test_app
Not sure if it makes any difference, but I use RVM for managing my rubies.
Sounds like your app is already using bundler and you have a bundler-inside-bundler problem. Try this:
Bundler.with_clean_env do
puts `bundle install`
end
I'm guessing what's happening is that your outer bundler sets the BUNDLE_GEMFILE env variable to your app's Gemfile, and then your inner bundler ends up inheriting it.
Please try this i will sure helps you.
rvm gemset create app_name
rvm use ruby-1.9.2#app_name
Ruby version what else you used.
then install bundler gem
gem install bundler.
Then bundle install and run server..

Resources