I have a cloned a ruby gem to my client.
According to the docs here (https://github.com/Jbur43/usps_counties)
I have to require 'usps_counties' in order to load it.
So my path is /usps_counties. From there I load irb and try requiring the usps_counties file, but it can't find it.
I then go to /usps_counties/lib (the file lives in the lib directory), load irb and try requiring it but cant find it.
What am I doing wrong here?
If you want to require a local file or gem in irb, I like this method:
irb -I lib -r usps_countries
This allows you to then require the module in your new irb instance:
require 'usps_countries'
Options used:
-I path Specify $LOAD_PATH directory
-r load-module Same as `ruby -r'
Have you tried a relative path?
require './usps_counties'
require_relative 'usps_counties'
Related
I have a script that needs to require specific files out of gems defined in the project Gemfile.
#!/usr/bin/env ruby
require 'some_gem/helpers/some_helper'
... rest of script
When I run the script, I get an error about not being able to load some_helper.rb. If I run with bundle exec command... then everything works.
I understand that bundle exec exposes the Gems to the $LOAD_PATH which lets require work. Is there a way to move that capability into the script so users don't have to type bundle exec?
Do I just need to add require "bundler/setup" to the script before I require the gem files?
http://bundler.io/v1.12/#getting-started
:)
#!/usr/bin/env ruby
require 'rubygems' # because reasons.. most probably it is not needed unless you are using really old ruby where it is not loaded by default
# also at the moment rubygems and bundler are being merged :)
require 'bundler/setup' # for things installed with bundler
require 'some_gem/helpers/some_helper'
You can also check e.g. http://mislav.net/2013/01/understanding-binstubs/
In order to work with rubyzip i installed the gems:
gem install --local rubyzip-1.1.7.gem
gem install --local zip-zip-0.3.gem
In my code i call the gems using the require method:
require 'zip/zip'
require 'zip/filesystem'
I want to use require to load the gems directly from their location on my machine.
i want somthing like this:
require 'path_to_my_zip_gem'
Where path_to_my zip_gem contains the gem files
Basically, it's because require method loads files by name from paths listed in $: or $LOAD_PATH
"If the filename does not resolve to an absolute path, it will be
searched for in the directories listed in $LOAD_PATH ($:)."
http://ruby-doc.org/core-2.2.2/Kernel.html#method-i-require
If you want to require a gem from the "local" path then the require_relative method could help since you can specify a path relative to the requiring file’s path. Look at the official documentation for further details:
http://ruby-doc.org/core-2.2.2/Kernel.html#method-i-require_relative
The simpliest answer is
Gem::Specification.find_by_name("GEM_NAME_HERE").full_gem_path
Example
> require File.join(Gem::Specification.find_by_name("railties").full_gem_path, "lib/rails/code_statistics.rb")
=> true
I have a gem that I created in Ruby on my local machine and I need to require this gem in a plain Ruby script that starts a service.
I have to require like this:
require_relative '../../../my-gem/lib/my/gem'
Is it possible to do this require without putting in the relative path?
require checks for files in $LOAD_PATH. You can put your gem in one of those directories in order to require it directly. If you don't like your load path, you can add a new directory to it in your script, or set the RUBYLIB environment variable which is added to the load path.
If you have a gem, you can install it and set the version you want (in my example 1.0.0.beta) with gem 'my-gem', '= 1.0.0.beta'.
But I think yu look for another solution:
You can extend the location where require looks:
$:.unshift('../../../my-gem/lib')
require('my/gem')
or
$LOAD_PATH.unshift('../../../my-gem/lib')
require('my/gem')
You could also use $: << '../../../my-gem/lib', but I prefer unshift. If your gem contains a file with similar names as in a gem (avoid it!), then unshift guarantees your script is loaded.
I am trying to require:
PROJECT_DIR/lib/shrt.rb
from:
PROJECT_DIR/test/shrt_test.rb
I tried:
require File.expand_path('.././lib/shrt.rb')
# and
require './lib/shrt.rb'
# and
require_relative '../lib/shrt.rb'
However it gives me an error stating it cannot load a file that is required in the shrt.rb file in lib/. In PROJECT_DIR/, I ran via Rake a Ruby test file:
ruby 'test/shrt_test.rb'
Any idea what I'm doing incorrectly?
Does:
require './lib/shrt.rb'
Not work? I've always required files in projects that way.
I needed to require the files in the following way:
require './lib/shrt/version'
require './lib/shrt/client'
...in the file I was trying to require in the test file. I required that file using require_relative '../lib/shrt'.
EDIT However now, when I rake install the gem, I'm using Bundler, and run it, I get `require': cannot load such file -- ./lib/shrt/version (LoadError).
EDIT 2 Fixed the problem by require_relative instead of just require on the version file.
I've got a wrapper for my Gem, socks, inside socks.rb. The entire file is made up of require statements, and a module declaration:
# lib/socks.rb
require 'socks/version'
require 'socks/base_controller'
require 'socks/templates'
require 'socks/tasks'
require 'socks/rake_tasks'
module Socks
end
However, require 'socks/tasks' and socks/rake_tasks is giving me a LoadError: no such file to load -- socks/tasks / rake_tasks error.
Is this a problem with the alignment of the require statements, or just the code?
Code is on Github: https://github.com/Beakr/socks
EDIT: require './socks/tasks' is now working, however require './socks/rake_tasks' is not.
Ruby load files using its $LOAD_PATH. This global array is changed by e.g. rubygems and bundler to allow to find libraries in various locations. In your sock.gemspec you have defined
gem.require_paths = ["lib"]
which means that rubygems will add the lib directory of your gem to ruby's $LOAD_PATH. But it odes so only if you have installed the gem and the gemspec is thus evaluated. If you don't want to install your gem, you can test your gem using
bundle exec irb
in your gem directory, or alternatively by first adapting your $LOAD_PATH in your irb session like so:
$LOAD_PATH.push "/path/to/your/gem/lib"
require 'socks'