How does load differ from require in Ruby? - ruby-on-rails

Is there any major difference between load and require in the Ruby on Rails applications? Or do they both have the same functionality?

require searches for the library in all the defined search paths and also appends
.rb or .so to the file name you enter. It also makes sure that a library is only
included once. So if your application requires library A and B and library B requries library A too A would be loaded only once.
With load you need to add the full name of the library and it gets loaded every time you
call load - even if it already is in memory.

Another difference between Kernel#require and Kernel#load is that Kernel#load takes an optional second argument that allows you to wrap the loaded code into an anonymous empty module.
Unfortunately, it's not very useful. First, it's easy for the loaded code to break out of the module, by just accessing the global namespace, i.e. they still can monkeypatch something like class ::String; def foo; end end. And second, load doesn't return the module it wraps the code into, so you basically have to fish it out of ObjectSpace::each_object(Module) by hand.

I was running a Rails application and in Gemfile, I had a specific custom gem I created with the option "require: false". Now when I loaded up rails server or rails console, I was able to require the gem in the initializer and the gem was loaded. However, when I ran a spec feature test with rspec and capybara, I got a load error. And I was completely bewildered why the Gem was not found in $LOAD_PATH when running a test.
So I reviewed all the different ways that load, require, rubygems and bundler interact. And these are a summary of my findings that helped me discover the solution to my particular problem:
load
1) You can pass it an absolute path to a ruby file and it will execute the code in that file.
load('/Users/myuser/foo.rb')
2) You can pass a relative path to load. If you are in same directory as file, it will find it:
> load('./foo.rb')
foo.rb loaded!
=> true
But if you try to load a file from different directory with load(), it will not find it with a relative path based on current working directory (e.g. ./):
> load('./foo.rb')
LoadError: cannot load such file -- foo.rb
3) As shown above, load always returns true (if the file could not be loaded it raises a LoadError).
4) Global variables, classes, constants and methods are all imported, but not local variables.
5) Calling load twice on the same file will execute the code in that file twice. If the specified file defines a constant, it will define that constant twice, which produces a warning.
6) $LOAD_PATH is an array of absolute paths. If you pass load just a file name, it will loop through $LOAD_PATH and search for the file in each directory.
> $LOAD_PATH.push("/Users/myuser")
> load('foo.rb')
foo.rb loaded!
=> true
require
1) Calling require on the same file twice will only execute it once. It’s also smart enough not to load the same file twice if you refer to it once with a relative path and once with an absolute path.
2) require returns true if the file was executed and false if it wasn’t.
3) require keeps track of which files have been loaded already in the global variable $LOADED_FEATURES.
4) You don’t need to include the file extension:
require 'foo'
5) require will look for foo.rb, but also dynamic library files, like foo.so, foo.o, or foo.dll. This is how you can call C code from ruby.
6) require does not check the current directory, since the current directory is by default not in $LOAD_PATH.
7) require_relative takes a path relative to the current file, not the working directory of the process.
Rubygems
1) Rubygems is a package manager designed to easily manage the installation of Ruby libraries called gems.
2) It packages its content as a zip file containing a bunch of ruby files and/or dynamic library files that can be imported by your code, along with some metadata.
3) Rubygems replaces the default require method with its own version. That version will look through your installed gems in addition to the directories in $LOAD_PATH. If Rubygems finds the file in your gems, it will add that gem to your $LOAD_PATH.
4) The gem install command figures out all of the dependencies of a gem and installs them. In fact, it installs all of a gem’s dependencies before it installs the gem itself.
Bundler
1) Bundler lets you specify all the gems your project needs, and optionally what versions of those gems. Then the bundle command installs all those gems and their dependencies.
2) You specify which gems you need in a file called Gemfile.
3) The bundle command also installs all the gems listed in Gemfile.lock at the specific versions listed.
4) Putting bundle exec before a command, e.g. bundle exec rspec, ensures that require will load the version of a gem specified in your Gemfile.lock.
Rails and Bundler
1) In config/boot.rb, require 'bundler/setup' is run. Bundler makes sure that Ruby can find all of the gems in the Gemfile (and all of their dependencies). require 'bundler/setup' will automatically discover your Gemfile, and make all of the gems in your Gemfile available to Ruby (in technical terms, it puts the gems “on the load path”). You can think of it as an adding some extra powers to require 'rubygems'.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
2) Now that your code is available to Ruby, you can require the gems that you need. For instance, you can require 'sinatra'. If you have a lot of dependencies, you might want to say “require all of the gems in my Gemfile”. To do this, put the following code immediately following require 'bundler/setup':
Bundler.require(:default)
3) By default, calling Bundler.require will require each gem in your Gemfile. If the line in the Gemfile says gem 'foo', :require => false then it will make sure foo is installed, but it won’t call require. You’ll have to call require('foo') if you want to use the gem.
So given this breadth of knowledge, I returned to the issue of my test and realized I had to explicitly require the gem in rails_helper.rb, since Bundler.setup added it to $LOAD_PATH but require: false precluded Bundler.require from requiring it explicitly. And then the issue was resolved.

Related

How to install pry/pry-rails globally for all projects?

How can replace irb and rails console with pry/pry-console globally for every project without having to include it in a project?
Easy mode is to hack your .irbrc, so that when anything tries to load IRB, you take over and force it to load Pry instead:
begin
gem "pry"
rescue => ex
$stderr.puts ex.message
else
require "pry"
Pry.start
exit!
end
If you're using Bundler, however, that'll still only work if Pry is available in the current bundle.
To make it work even when Bundler doesn't think Pry should be allowed to activate, you'll need to monkey with gem loading -- Bundler tries very hard to make it impossible.
To do that, you'll need a ~/.rubyrc.rb file, which you ensure always gets loaded for all ruby commands by exporting RUBYOPT=$HOME/.rubyrc in your .bashrc / .zshrc.
That file can then hack into Bundler's internals to force loading a non-bundled gem. You can also monkey-patch Bundler.require, which is how Rails loads all the gems in the Gemfile, to similarly force pry-rails in any application that contains rails.
(This strategy also allows you to globally support binding.pry, without needing to explicitly require anything, or add the gem to the project.)

Can I use shared gem in rails directory?

I want to use one gem installed in system directory at a Rails project.For a certain reason, I cannnot add it to Gemfile and bundle install.Basicaly, I use bundler.
I want to do like this,
require 'gem_installed_in_vendor/bundle'
require 'gem_installed_in_system'
I wonder removing disable_shared_gems option help my hope,and I delete it from .bundle/config.But,it doens't work hopely.
output of bundle config is,
Settings are listed in order of priority. The top value will be used.
path
Set for your local app (/Users/my_name/works/thisApp/.bundle/config): "vendor/bundle"
and which gem's output is,
/Users/my_name/.rbenv/shims/gem

Requiring a gem's assets from another gem

I am creating a gem that will contain the foundation-rails gem along w/ some common variables that are used across my applications. I have created a stylesheet at vendor/assets/stylesheets/foundation.scss. I load this from within my application as such
Gemfile
gem 'foobar-foundation-rails', path: '...'
app/assets/stylesheets/application.css
//= require foundation
This is a good starting point but how do I include the foundation-rails gem's stylesheet from within this file? I am unsure how to reference another gem's assets
I think the best approach is to put the responsibility for the require statements in your rails app's javascripts file. This is most likely not functionality you want to bury in a gem, as it hides what is happening.
Then make sure you require your gem's css file before the foundation-rails require. However you should put a dependency requirement in your gem's gemspec file to ensure that the foundation-rails gem will be installed by bundler when your gem is installed.
Also you may have to "namespace" your gems style sheet in order to avoid namespace collisions.
vendor/assets/stylesheets/foobar_foundation_rails/foundation.css
Which would change the require in your stylesheet file to
require 'foobar_foundation_rails/foundation.scss'
Lastly, the naming of a gem establishes how the gem gets required. When you use dashes Rails expects things to be required, and hence your gem's directory structure to follow
lib/foobar/foundation/rails
As opposed to an underscore naming foobar_foundation_rails
lib/foobar_foundation_rails
Unless you're going to build an "extension" to the foundation-rails gem, which would need to be called foundation-rails-foobar, you may want to go with the underscore syntax to save yourself some require headaches. The devise gem is a good example of extension gems.

How to require a gem in .irbrc without needing to also add it to a Rails Gemfile?

I've added awesome_print to my ~/.irbrc file like so:
require 'ap'
Inside a Rails project directory, if I run irb it loads the gem fine, because I've already installed the gem locally. But if I run rails console, it spits out this error:
cannot load such file -- ap
How can I resolve this? I am guessing that it's looking for the gem in the app's Gemfile, but I don't want to add it to the Gemfile because I don't want other developers requiring that dependency. I only want to use awesome_print on my machine.
I am also using rbenv, if that is of any help.
There is this trick.
What you need to do is
# Copy the definition of the debundle! method into your ~/.irbrc
# Call 'debundle!' from IRB when you need to.
(as explained at the top of the file)
The text as it appears on the referred to site:
debundle.rb allows you to require gems that are not in your Gemfile when inspecting
programs that are run with Bundler.
Use at your own risk!
Paste the code of debundle.rb and you are done! A good place would be your .irbrc file
before requiring irbtools.
The code is directly taken from pry-debundle.
Please look there for any further information. This repo exists to simplify debundling
without using the pry repl.

How am I supposed to use bundle install --standalone with a Rails app?

bundle install --standalone seems like a wonderful idea, but I'm having a hard time wrapping my head around how to use it that doesn't wind up requiring you to have rubygems or bundler installed.
It generates a bundler/setup.rb which adds the lib and ext directories of my gems, seemingly in order. Presumably, all I'd need to do is add it to the load path, and all's well.
But bundler/setup.rb doesn't seem to actually require anything.
Now, that's fine, because the normal bundler/setup doesn't require anything either and leaves it to the app to call Bundler.require
Rails by default does the requiring with this little ditty:
if defined?(Bundler)
Bundler.require(*Rails.groups(:assets => %w(development test)))
end
At the point it hits this, Bundler isn't defined (bundler/setup.rb doesn't define it), so it skips over the block.
So how exactly do I require bundler. If bundle install --standalone actually bundled bundler, presumably, I could manually call require bundler and then have Bundler defined, but it seems to exclude itself from the bundle.
Is there an app out there that actually uses bundle install --standalone, and if so, how?
To get this to work with Rails, you remove the Bundler.require call and manually add all of the require lines where they're needed.
This has pros and cons. On the plus side, it can make loading your app faster as not all the gems have to be required at load time. It also makes clear what gems are being used where.
On the down side, you have to add all the require calls to your application.
Take a look at Myron Marstons blog post for a better explanation.
bundle install --deployment #install ruby & bundled gem into project's directory

Resources