I replaced rvm with rbenv on my Macbook and after doing so I've needed to add require 'rubygems' to things to get them working.
Is there a way of forcing rubygems to be required, or removing the need for it?
For sanity reasons you should always require what you need. As rubygems is included by default in ruby 1.9 you should do something like:
require 'rubygems' unless defined?(Gem)
at the top of your script.
Related
Why am I not able to use these lines in a Gemfile:
gem 'date'
gem 'pp'
Must these be required in file instead like this:
require 'date'
require 'pp'
Or is there a way to mix them into your Gemfile so they are available project wide?
I think that date and pp are part of ruby 1.9.2 core and as a result are different from regular gems but I don't exactly understand why...
Because those are not Gems but part of the Ruby standard library. But the standard library isnt loaded by default, hence the require statements
This is related to this question:
https://stackoverflow.com/questions/3179797/how-to-use-rubytorrent-or-other-gems
I thought RubyGems is a package manager, similar to apt-get on Ubuntu...
So when do we need to require 'rubygems' in our code?
Use require 'rubygems' when you are using a gem that you installed with Rubygems. The reason is that Ruby uses Rubygems to determine the path of the gem that Rubygems installed. (is unable to locate the gem you want to use)
Alternatively, you can pass the -rubygems flag when you invoke your script, or set export RUBYOPT=rubygems in your profile (~/.bashrc or ~/.bash_profile or ~/.profile) which is basically the same as the flag, except it is implicit.
On 1.9, rubygems is required implicilty, and you shouldn't have to do that.
Here are some docs about it http://docs.rubygems.org/read/chapter/3
Note: Some have built solutions (zozo and faster_rubygems) to avoid Rubygems overhead http://www.ruby-forum.com/topic/212463
I have seen many samples of Ruby code with this line (for example, http://www.sinatrarb.com/). What is purpose of this require?
# require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello world!"
end
In all cases the code works without this line.
require 'rubygems' will adjust the Ruby loadpath allowing you to successfully require the gems you installed through rubygems, without getting a LoadError: no such file to load -- sinatra.
From the rubygems-1.3.6 documentation:
When RubyGems is required, Kernel#require is replaced with our own
which is capable of loading gems on demand.
When you call require 'x', this is what happens:
If the file can be loaded from the existing Ruby loadpath, it
is.
Otherwise, installed gems are searched for a file that
matches. If it's found in gem 'y', that gem is activated
(added to the loadpath).
The normal require functionality of returning false if that file
has already been loaded is preserved.
See the documentation for Kernel#require to understand why this is necessary.
It is often superfluous. It will allow you to require specific versions of particular gems though, with the gem command.
https://guides.rubygems.org/patterns/#requiring-rubygems
As an addition to prior (and correct answers): Ruby 1.9 and newer ship with RubyGems built-in, so there is no real need to require 'rubygems'. Source here
I`ve encountered the problem after updating some gems, so basically all older gems are still available but i cant force application use them.
Lets say, i need something like that:
require 'rubygems'
require 'mygem', '0.1.2'
Is there a way to do it?
Well, always happens to me. Found an answer.
require 'rubygems'
gem 'mygem', '=0.1.2'
require 'rubygems'
require 'activerecord', '=1.15.3'
This worked for me.
Check this out. Seemed helpful
Link
I do something like sudo gem install json. Then I do irb. Then I do require 'json'. Then it says no such file to load -- json
You need to make sure that RubyGems itself is loaded, for requiring gems to work.
There are several ways to do this. You could require 'rubygems' explicitly in each file you want to use the gems in, but that might get to be a pain. Or you could pass -rubygems into ruby when you execute it, but again, you'd need to remember to do that each time.
The best way would probably be to set the RUBYOPT environment variable to rubygems. For instance, you may add the following line to your .profile:
export RUBYOPT=rubygems
Try adding this first:
require 'rubygems'
RubyGems is a dependency manager as well as an installer.