Questions tagged [require]

In interpreted languages like Lua, PHP and Ruby, "require" is a statement that tells the interpreter to include a certain source-file at that position where the "require" statement has been placed.

Require in PHP

In PHP there exist the statements include() and require() to include another file with PHP-code into your document.
The difference to include is, that require will throw an error if the file is not available.

A variant of require() is require_once(). This includes a file only, if it hasn't already been included before.

require('/path/to/script.php');

Require in Ruby

In Ruby, the require method does what include does in most other programming languages: It will include and 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.

require '/path/to/script.rb'

The include and require methods do very different things. Please check the source for further information on include.

Source: http://ruby.about.com/b/2008/10/23/a-quick-peek-at-ruby-include-vs-require.htm

Require in JavaScript

JavaScript has no native support for the require statement. However, there exist various implementations that try to mimic the behaviour of other programming languages.

These implementations are not fixed to use the word "require" for their loading routines, but may use other statements.

Some of them are:

  • RequireJS

    require(["moduleA", "moduleB"], function(A, B) {
       // do something with moduleA ...
       A.doSomething();
       // do something with moduleB ...
       B.doSomething();
    });
    
  • Head JS

    head.js("/path/to/script.js");
    
  • LABjs

    $LAB.script("/path/to/script.js");
    
  • jQuery Plugin

    jQuery.require("/path/to/script.js");
    

Require in Lua

The require statement of Lua searches for a module with the given name automatically extended by a previously defined path pattern.

The path patterns are rules that specify how to construct a path name using the parameter given to require. After path construction, require will check if one of the constructed paths is valid in order to load this path.

The pattern usually includes an expression, which extends the given name by '.lua', Lua caches already loaded modules, so they won't be loaded twice.

    require "module"
2940 questions
41
votes
10 answers

React Native: require() with Dynamic String?

I have read several posts about issues that people are having with React Native and the require() function when trying to require a dynamic resource such as: Dynamic (fails): urlName = "sampleData.json"; data = require('../' + urlName); vs. Static…
Jake Chasan
  • 5,528
  • 5
  • 37
  • 81
37
votes
3 answers

require 'rubygems'

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.
demas
  • 40,368
  • 53
  • 163
  • 263
35
votes
4 answers

How to require a ruby file from another directory

I am trying to require a rake file that I have created inside another file I have. These two files are inside two different directories. I have require at the top of my first file with the name of the second file inside the quotes after the require.…
33
votes
6 answers

How to do multiple imports in Python?

In Ruby, instead of repeating the "require" (the "import" in Python) word lots of times, I do %w{lib1 lib2 lib3 lib4 lib5}.each { |x| require x } So it iterates over the set of "libs" and "require" (import) each one of them. Now I'm writing a…
Eduardo
  • 333
  • 1
  • 3
  • 5
33
votes
6 answers

How to include a class in PHP

I have file index.php, and I want to include file class.twitter.php inside it. How can I do this? Hopefully, when I put the below code in index.php it will work. $t = new twitter(); $t->username = 'user'; $t->password = 'password'; $data =…
CLiown
  • 12,698
  • 45
  • 117
  • 194
31
votes
5 answers

Browserify with require('fs')

I was trying to use browserify on a file that uses the fs object. When I browserify it, the call to require('fs') doesn't get transformed and require returns {}. Is there any workaround for this? I've seen some suggestions on stackoverlow and…
Fred Finkle
  • 1,697
  • 2
  • 17
  • 21
30
votes
7 answers

Neither ruby and nor irb can load .rb file in current directory

I'm having a really noob problem with importing files in Ruby. I'm making a Ruby app in Windows XP. All the class files for the app are in "C:/Documents/Prgm/Surveyor_Ruby/lib". But when I require a file in another file, neither ruby nor irb can…
Bad Request
  • 3,810
  • 5
  • 29
  • 35
30
votes
1 answer

Ruby: Require a file again if it is changed

I've written a gem that looks in config/ for a config file. This works fine but if the user changes any config they have to stop the program and start it again before my gem loads the new config. This would require them to restart the whole rails…
Arcath
  • 4,083
  • 8
  • 36
  • 69
30
votes
3 answers

How to create include files in Lua language?

I want to create a header file in Lua (header.lua), then execute the require function to load it. How do I execute require to a file that I have created?
Fernando Pinheiro
  • 1,706
  • 2
  • 16
  • 21
30
votes
6 answers

PHPStorm: undefined variables caused by include/require

PHPStorm showed that all the variables from other files, both required and included, are undefined. I found this solution here, but after I disabled that option Ignore 'include' and 'require' statements, the IDE ignored all undefined variables. For…
kxc
  • 1,018
  • 2
  • 14
  • 35
30
votes
4 answers

In Perl, what is the difference between use and require for loading a module?

What is the difference between doing use My::Module and require My::Module?
Sam Lee
  • 8,493
  • 15
  • 44
  • 54
29
votes
1 answer

Rails lib directory

Question about lib directory. What are good practices in using the lib directory? When should it be used over app/models or app/helpers? And somewhat related how do you get Rails 3 to include files from the lib directory? Thanks
GTDev
  • 5,258
  • 8
  • 45
  • 80
29
votes
2 answers

how to require active record working outside of rails

i need to require active record, but I am working outside of rails (here is why: Simple Ruby Input Validation Library). do I need to require the entire rails gem, or can i be DRYer?
user276712
  • 822
  • 4
  • 12
  • 20
28
votes
4 answers

file_exists() or is_readable()

Which one should you use when you want to include a PHP file? if(file_exists($file) require "$file"; or if(is_readable($file) require "$file"; ?
Co za asy...
  • 281
  • 1
  • 3
  • 3
28
votes
5 answers

Swift REPL: how to import, load, evaluate, or require a .swift file?

In the Swift REPL, how to import (a.k.a. load, evaluate, require) a typical text *.swift file? I want to use the code from this file: ~/src/Foo.swift Syntax like this doesn't work: import ~/src/Foo.swift For comparison: An equivalent solution in…
joelparkerhenderson
  • 32,633
  • 18
  • 90
  • 113