Ruby file locations and requiring them - ruby

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.

Related

After adding a Ruby Gem to a Gemfile and running bundle exec file.rb, I still get a file not found error

This is in my Gemfile:
gem "trinsic_service_clients", "~> 1.1"
This is at the top of my file named instantiate_clients.rb
require_relative 'trinsic_service_clients
However, when I run bundle exec ruby instantiate_clients.rb I still get this error:
Traceback (most recent call last):
1: from instantiate_clients.rb:1:in `<main>'
instantiate_clients.rb:1:in `require_relative': cannot load such file -- /home/runner/DefenselessGrowlingExtraction/trinsic_service_clients (LoadError)
When I type bundler exec gem which trinsic_service_clients I get the following:
/home/runner/DefenselessGrowlingExtraction/.bundle/ruby/2.5.0/gems/trinsic_service_clients-1.1.5018/lib/trinsic_service_clients.rb
require_relative is for code in your application. It is not for code stored in gems. For those you should use require as it will look through $LOAD_PATH for the required files.
The easy way to make use of Gemfile is to load everything in via one shot:
require 'bundler'
Bundler.require(:default)
This not only adds the gem declarations, but does default require calls as well, as in this should already require your declared gem.
There's also require 'bundler/setup' which only adds the load paths, you still need to require, but this can help minimize load times if you may not necessarily need all the gems.
require 'bundler/setup'
require 'trinsic_service_clients'
This can be useful if your Gemfile has a lot of dependencies, but the scripts you're running may only use a small subset of them.

Load File From Local Gem in IRB

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'

Require files from gem in Ruby script without bundle exec

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/

Issues in loading gems from env.rb file

I have 2 env.rb file(for desktop browser and mobile browsers) which has all the required environment related setups done.
The below line is present in both env.rb files but it does not work when included in one of the file(mobile browser). These are the standard gems used.
require 'time_diff'
require 'rubyXL'
require 'Prawn'
All though the files are exactly the same except including few more external libraries, we get the below error when running the Appium script.
cannot load such file -- time-diff (LoadError)
Any idea where to look for the problem?
EDIT:
The difference in both the files are below. Apart from these lines, other lines are related to normal variable assignment and config file handling.
File 1:
$LOAD_PATH<< File.expand_path('../features/DesktopWeb/pages', 'common.rb')
File 2:
$LOAD_PATH<< File.expand_path('../features/MobileWeb/pages', 'common.rb')
I faced same issue and i tried the below steps that resolved my issue.
Add all gems in Gemfile.
require 'time_diff',
require 'rubyXL',
require 'Prawn',
run "gem install bundler" this will install the bundler.
run "bundle install" from the project directory where the Gemfile is placed.
This will install all the gems and there dependency gems.
Then try to run your code...
Hope this will resolve your issue.

Strange require statement errors in Ruby?

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'

Resources