(...)in `require': no such file to load -- 'gemname' (LoadError) - ruby

I haven been all day training to fix this problem but i couldn't.
The question is easy, i don't want to put anymore the require 'rubygems' line everytime i require a gem...
If i put the require 'rubygems' before any other "require" the file runs perfect but if i don't put the require 'rubygems' line the following error occurs:
(...)in `require': no such file to load -- 'gemname' (LoadError)
I suspect that may be there is some path remaining where to check out the gems repository.
I want to ask you if there is a way to do this.
Thanks a lot.
Cheers,
Juan.

You could invoke you ruby script with
ruby -rubygems script.rb
or add rubygems to RUBYOPT
$ export RUBYOPT="rubygems"

put
require 'rubygems'
as first line of your ruby code and be safe.
of course you can invoke with -rubygems switch (as Peter Krenn wrote) instead of it

In Unix you can:
$ RUBYOPT="rubygems"
$ export RUBYOPT
$ ruby juans_masterpiece.rb
and in Windows:
SET RUBYOPT=rubygems
or right-click on My Computer->Properties->Advanced->Environment Variables
and then finally add the RUBYOPT variable there. Next time you open a cmd.exe run set and it will be there.

You don't have to put it every time you require a gem — you just have to have it before the first time you require a gem. When you require Rubygems, it replaces the default require with a special one that does all the Rubygems magic.
But that's only in 1.8. You don't have to require Rubygems at all in Ruby 1.9 — so that's a very easy solution to the problem as long as you aren't dependent on 1.8-specific things.

Right-click the Computer Icon, then select Properties, then Additional system parameters, then Environment variables, there is a GUI for changing opts, click Create, put name and value, OK. This is an approximate translation of how you do this on windows 7, if you can't find the place try to google for "changing environment variables in {your windows version here}"

Related

Automatically load Dotenv on my ruby console

I'd like to automatically run Dotenv.load('.env.development') whenever I launch up a ruby console, it could either be from bundle console or alternatively irb. I'm using Sinatra, not Rails, and I'm not sure how to run some commands on console start.. I'd prefer to do this without a bash script, instead using the internal capabilities of the tools.. If there's a place to put ruby code on the start of a ruby console that would solve my issue and also allow for future console customization.
You can create a .irbrc file in our project's directory which is loaded automatically when an IRB session is started. And add something like this to that file:
begin
require 'dotenv'
Dotenv.load('.env.development')
rescue => e
puts "loading Dotenv failed. because: #{e.message}"
end
Read more about the .irbrc file in the Ruby-Docs.
You try folliwing the documentation of gem(sorry my ignorance I don't know anything about sinatra)?:
Documentation dotenv
Install the gem:
$ gem install dotenv
By default, load will look for a file called .env in the current working directory. Pass in multiple files and they will be loaded in order. The first value set for a variable will win.
require 'dotenv'
Dotenv.load('file1.env', 'file2.env')
In your case i think should be:
require 'dotenv'
Dotenv.load('.env.development')
In ruby vanilla I dont know if possible, I think yes.
One option is to creating a ./bin/console script ala Bundler's gem.
I created this bin/console file as a temporary solution, but I'm curious whether I can get #spickermann's answer (which I incorporated here) with irbrc to work with a same-directory .irbrc
#!/usr/bin/env ruby
begin
require 'dotenv'
Dotenv.load('.env.development')
rescue => e
puts "loading Dotenv failed. because: #{e.message}"
end
require "irb"
IRB.start(__FILE__)

Ruby: require works in gem, fails when running from source

Try creating a gem based on bundler's official guide on developing a Ruby gem.
Running bundle gem foodie will create a structure and generate files in the lib directory:
foodie
version.rb
foodie.rb
foodie.rb reads
require "foodie/version"
module Foodie
# Your code goes here...
end
Running ruby lib/foodie.rb (or also from different directories) will result in
C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- foodie/versio
n (LoadError)
from C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from foodie.rb:1:in `<main>'
On the other hand installing the gem via rake install and then requiring the gem works just fine.
It works from source if require "foodie/version" is changed to require_relative "foodie/version" in foodie.rb. As I understand
require works based on modules
require_relative works based on directory structure
To me the latter looks like a hack. It'd no longer make sense to structure your code via modules as it wouldn't be enforced (maybe it'd still make sense but you could make mistakes and never notice).
My questions are:
Is it possible to test a gem from source without installing it while following the bundler convention (using require instead of require_relative)?
Why does the gem work after installed?
Is there any best practice for the usage of require, require_relative, modules, files and general structure?
Thank you.
You need to add your lib dir to Ruby’s load path. The load path is a list of directories that Ruby searches for files in when you call require. Rubygems also manages the load path when you are using gems, which is why your code works when installed as a gem.
You say “as I understand ... require works based on modules”, this is not correct. require works with files, it’s just convention that a class or module is defined in a file with a matching name, e.g. MyModule might be in my_module.rb.
There are a few ways to add a dir to the load path. From the command line you can use the -I option:
$ ruby -I lib lib/foodie.rb
If you wanted to avoid typing -I lib you could use the RUBYLIB environment variable. Ruby adds the contents of this to the load path:
$ export RUBYLIB=lib
$ ruby lib/foodie.rb
(On Windows I think you will need to use set rather than export.)
You can also manipulate the load path from withing the program itself. It is stored in the global variable $LOAD_PATH, aliased as :$. This is how Rubygems and Bundler manage your gems.

RubyGems - require, file location and (load error) complications

very new to coding so, having exhausted Google and Stack Overflow, would really appreciate some advice...
I am currently building a web-scraper to get familiar with CMD vs Sublime Text, feeling Ruby in action; So i am working my way through this tutorial
After having actioned in CMD
C:\gem install HTTParty
SUBLIME TEXT - starts with this code:
require_relative 'HTTParty'
require_relative 'Nokogiri'
etc
But before i can get to anything more from CMD, i hit web_scraper.rb and it returns with:
C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `require_relative': cannot load such file -- C:/Users/ATH18/Desktop/nokogiri_tutorial/httparty (LoadError)
from C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `<main>'
[Finished in 0.1s with exit code 1]
I think this has to be due to one of the following:
i) maybe gems have to have their actual files dragged into whatever folder you're creating a new program in?
ii) i'm missing another piece of information that would let it run properly?
iii) perhaps there's another way to tell CMD/ruby that the "require"d gem is not in the current folder (I read this somewhere but their advice didnt seem to work either).
NOTE - i have done gem install xxxxxx in both the C:\ directory and C:\users\desktop\projectFolder\
Help?
You have to use require instead of require_relative. The difference between both a is explained here: https://stackoverflow.com/a/3672600/92049
Use require 'GEMNAME' for gems installed with gem install GEMNAME; use require_relative 'PATH' to require a file relative to the file containing require_relative. (Most often you will find yourself using require.)
To come back to your question: As it says in the tutorial, you have to write require 'HTTParty' instead of require_relative 'HTTParty'.
Does this answer your original question?

ruby require './blahblah.rb' vs require File.expand_path('../blahblah', __FILE__)

What is the difference between
require 'blahblahlblah.rb'
vs
require './blahblah.rb'
vs
require File.expand_path('../blahblah', __FILE__)
I see both of them being used. Wondering what's better, and under what circumstance is one better than the other.
Thanks!
require blaba.rb is searching to your default gem path to load the file, which depends on the ruby version you are using. For example RVM will search in $HOME/.rvm/rubies/... while a system wide ruby will search in the distribution's default path. Note that this is where gems are located, but you could manually add a library say mylibrary.rb in the same path and use it in any of your programs. However, that's an awful thing to do, it's a much cleaner procedure to create gems and install them in your system.
require ./blabla.rb loads a file that is sitting in your working directory. You could add the full path like require /home/username/library/myproject/models/sample.rb. It will work just about the same. In the UNIX-like world the ./ sign means current directory. This solution is often used in irb to load say a rails Model i.e. users.rb into irb or pry and work with it. To give you an example in a shell environment (if you are familiar with UNIX shells, you'll figure it out):
GreyJewel ~ » ls myports.txt
myports.txt
GreyJewel ~ » ls ./myports.txt
./myports.txt
The third solution require File.expand_path('../sample.rb', __FILE__) is used in programs, because it explicitly creates a full path using as an anchor the directory which the file holding the line sits, which is a much more secure approach compared to require ./sample.rb. Note that when you load a ruby file, you can omit the file extension .rb.
Hope this clarifies a bit the situation.

Ruby on Windows path for requires in dir not working

I've a small ruby program that require files in the same directory. Program works perfect on my mac and when I run a test ruby script without any require it also works so. It seems the ruby program doesn't look in the current directory for the file by default. e.g. the . dir. In windows where do I need to update this so ruby does look in the current dir for requires?
Chances are that your Mac is running Ruby 1.8 and Windows is running Ruby 1.9. As of 1.9, the default load path no longer includes the current directory. A common practice is to add this to the top of your ruby file before your require statements
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'my_file.rb'
You can also use the shorthand $: instead of $LOAD_PATH:
$:.unshift File.dirname(__FILE__)
Another alternative is adding the load path on the command line instead:
ruby -I. my_ruby_file.rb
Ok, I understand now since 1.9.2 for "Security" reasons they don't allow require to work like that anymore. The neatest way I found to solve it strangely was to put './' in front of every require.
e.g.
require "./myfile.rb"
"." was removed from $: was removed from Ruby 1.9.2 to be precise. Do
puts RUBY_VERSION
puts $:.inspect
on Ruby 1.8 (what's installed on your Mac) and Ruby 1.9.2 (what's installed on your windows machine) if you don't believe me.
Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative? discusses why "." was removed.

Resources