When to Call a "Require" in Rails

When to call a require in Rails?

If you're concerned about performance then you should require things in the context of where they are needed so that if that portion of your code is not exercised, the library is not loaded. Any subsequent calls to require have no effect as that file has already been loaded. This ends up looking like something along the lines of:

if (user.using_openid?)
require 'openid'

# ... Do OpenID stuff
end

While this is more efficient in terms of resources, it can make it very difficult to determine the dependencies of your application. Declaring these up-front makes it clear to other people maintaining the software. Keep in mind that "other people" always includes your future self when you've forgotten about some details of your application.

You're technically allowed to require anything at any time, late or early, but declaring your requirements up front is better from a design perspective. If you find that there is an element that is used only intermittently and takes an unusual amount of time or memory to load, then you should probably document that up front in your requirements file. For example:

require 'library1'
require 'library2'
require 'library3'
require 'library4'
require 'library5'

# Other libraries loaded as required:
# * slowimagelibrary
# * slowencryptionlibrary
# * openid

Arguably this is less of an issue with bundler because you can have your gems declared up front more formally and the actual require call can come later.

What is the difference between include and require in Ruby?

What's the difference between
"include" and "require" in Ruby?

Answer:

The include and require methods do
very different things.

The require method does what include
does in most other programming
languages: run another file. It also
tracks what you've required in the
past and won't require the same file
twice. To run another file without
this added functionality, you can use
the load method.

The include method takes all the
methods from another module and
includes them into the current module.
This is a language-level thing as
opposed to a file-level thing as with
require. The include method is the
primary way to "extend" classes with
other modules (usually referred to as
mix-ins). For example, if your class
defines the method "each", you can
include the mixin module Enumerable
and it can act as a collection. This
can be confusing as the include verb
is used very differently in other
languages.

Source

So if you just want to use a module, rather than extend it or do a mix-in, then you'll want to use require.

Oddly enough, Ruby's require is analogous to C's include, while Ruby's include is almost nothing like C's include.

Do Ruby 'require' statements go inside or outside the class definition?

Technically, it doesn't really matter. require is just a normal method call, and the scope it's called in doesn't affect how it works. The only difference placement makes is that it will be executed when whatever code it's placed in is evaluated.

Practically speaking, you should put them at top so people can see the file's dependencies at a glance. That's the traditional place for it.

Ruby on Rails - how require is executed in application.js

What kind of JavaScript syntax is this.

Anything starting with a // is a Javascript comment.

How is it able to process it ?

Sprockets on the server side scans the JS file for directives. //= is a special Sprocket directive. When it encounters that directive it asks the Directive Processor to process the command, require in this example. In the absence of Sprockets the //= require .. line would be a simple JS comment.

Ruby require vs Sprockets require

These are two completely different things. The one you link to is Ruby's require.

Why not use script tags to load JS files.

Usually, you want to concatenate all your app JS files and then minify them into 1 master JS file and then include that. I recommend reading the YSlow best practices on this.

I also recommend watching the Railscasts on Asset Pipline - http://railscasts.com/episodes/279-understanding-the-asset-pipeline

Cheers!

Ruby on Rails require doesn't work?

While require is similar to Java's import, it doesn't have any of the namespace manipulation stuff that import provides. If you really want to have shorter references, you'll need to create them yourself.

class MyController < ApplicationController
UserBase = FromDb::Users::UserBase

def default
user = UserBase.new
# ... etc
end
end

Also, since this is a Rails application, you don't need the explicit call to require (and it's better if you leave it off). If you name everything following the standard conventions, Rails will require the file for you automatically, then reload it whenever the file changes. If you do a manual require you'll lose the autoreloading.

How does load differ from require in Ruby?

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.



Related Topics



Leave a reply



Submit