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
Related
If I understand correctly puting
require 'rubygems'
require 'bundler/setup'
at the top of my ruby source file will ensure that subsequent requires will load the versions of the gems specified in the Gemfile located in the same directory, thus allowing me to develop multiple projects which use different versions of the same gem - is this correct? How is this different from using Bundler.setup()?
When you require 'bundler/setup' it loads this file which calls Bundler.setup for you. This means there's no need for you to include that in your own code.
RubyGems is loaded implicitly by Ruby, unless you're using a really old version, so require 'rubygems' isn't needed.
I'm trying to require the new (i.e. not the one bundled with ruby) version of test/unit. As per the instructions I installed it with gem i test-unit. But now when I require test/unit I seem to be requiring the old version. For example, I don't have the method Test::Unit.at_start. This happens even when I explicitly require the full path to the new test/unit installation.
Is there any way for me to investigate which symbols are being loaded and why?
Since Ruby comes with an implementation of test/unit (it’s actually a Minitest compatibility wrapper) calling require 'test/unit' will load that version, since it is already on the load path. Calling require '/absolute/path/to/test/unit' will load the new file, but when that file calls e.g. require 'test/unit/testcase it will load the files from stdlib, resulting in a mix of classes (so don’t do that).
What you need to do is activate the gem with the gem method before you require it. That will ensure the gem lib path is on your load path before the stdlib, so require will find those files first.
gem 'test-unit' # You can also pass a specific version as a second
# argument to `gem` if you want.
require 'test/unit' # This will find the gem version.
# Now the gem version will be loaded.
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'
I'm trying to use rake in my ruby script...(Ruby 1.8.6, JRuby 1.6.5)
Downloaded rake using gem install --remote rake, looks ok on install...
Fetching: rake-0.9.2.2.gem (100%)
Successfully installed rake-0.9.2.2
1 gem installed
I've got a simple ruby script which works fine, but when I import rake to using any of the following requires, it starts complaining....
require 'rake'
LoadError: no such file to load -- rake
or
require '/lib/rake'
LoadError: no such file to load -- lib/rake
After some searching, I found that adding require 'rubygems' just before rakefixes the issue....
require 'rubygems'
require 'rake'
Even though it's working, I've got some questions...
The gem spec on rake shows the require_path as lib, so why
doesn't require '/lib/rake' work? Am I misunderstanding the significance of require_path?
Why is it necessary to place require 'rubygems' before require
'rake'
Yes, you are misunderstanding the significance. The require_paths in the specification is an array of subdirectories of that gem's installation directory that should be searched for files belonging to the gem.
To find out where rake really is, try this:
$ gem which rake
You'll see that it is actually installed somewhere completely unrelated to /lib; on my system, it's under /var/lib/gems. Ruby gems, in general, live in their own directory structure; the only file in the standard Ruby include path ($:) is rubygems itself, which you used to have to explicitly require in order to make any actual gems visible. (Since Ruby 1.9, that has not been necessary.)
Gems are more complex than just libraries to load; you can have multiple versions of the same gem installed, and specify which one you want at a time, and do other things that wouldn't work if the gems were just dumped into the standard Ruby include path.
The require_path in the gemspec tells ruby where the files of this gem are located within the gem. It makes you able to type require 'rake', and ruby then knows it needs to look for /lib/rake within the gem installation folder.
In Ruby 1.8, rubygems (the mechanism responsible for making gems available to your app) is not loaded by default, and the default ruby isn't aware of any gem on your system. You need to load rubygems before being able to require any other gem. This is not the case anymore with Ruby 1.9.
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