# Require and include?

**URL:** <https://www.ruby-forum.com/t/require-and-include/186028>\
**Category:** Ruby\
**Created:** [March 30, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/require-and-include/186028 "2010-03-30T17:05:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rantingrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rantingrick](https://www.ruby-forum.com/u/rantingrick)\
**Post date:** [March 30, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/require-and-include/186028/1 "2010-03-30T17:05:39Z")

</div>

Hello,

I am having trouble understanding the difference between require and  
include. Also i need to know how to import a certain function or class  
into my current namespace.

Now before i go any further i should explain that i am a Python  
programmer and i understand that whilst Ruby and Python share some  
similarities there are major differences between the languages, and  
that is what seems to have me stumped here.

In python we use the “import” statement to bring modules, functions,  
whatever. And the import statement can function in many ways,  
observe…

if i want to bring all the names from the math module into my current  
namespace without the need to qualify the names i can say…(note:  
dir() shows the current namespace!)

> > > dir()  
> > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’]
> 
> > > from math import \*  
> > > dir()  
> > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’, ‘acos’,  
> > > ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’,  
> > > ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘exp’, ‘fabs’, ‘factorial’,  
> > > ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘hypot’, ‘isinf’, ‘isnan’, ‘ldexp’,  
> > > ‘log’, ‘log10’, ‘log1p’, ‘modf’, ‘pi’, ‘pow’, ‘radians’, ‘sin’,  
> > > ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘trunc’]
> 
> > > hypot(3,4)  
> > > 5.0

Now if i want to just use a certain function, class, constant, i can  
call for just those specific names like…

> > > dir()  
> > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’]
> 
> > > from math import hypot, pi  
> > > dir()  
> > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’, ‘hypot’, ‘pi’]
> 
> > > pi  
> > > 3.1415926535897931
> 
> > > hypot(3,4)  
> > > 5.0
> 
> > >

Now if i want to bring a reference to the module in and then qualify  
every thing within the module by perpending the  
“module\_name.identifier” i can say…

> > > import math  
> > > dir()  
> > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’, ‘math’]
> 
> > > math.hypot(3,4)  
> > > 5.0

Ok, i know Ruby is different, so tell me how i do the same thing only  
in Ruby terms. Specifically i need to bring in a function that resides  
in a module in a different file. How do i do this?

---

<div class="post-metadata">

**Author:** ![rantingrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rantingrick](https://www.ruby-forum.com/u/rantingrick)\
**Post date:** [March 30, 2010, 5:42pm UTC](https://www.ruby-forum.com/t/require-and-include/186028/2 "2010-03-30T17:42:17Z")

</div>

Hello,

Ruby is different of other languages as php. In php, “include” an  
“require” put the content of a the file “called” into the “caller” file.

Ruby “require” statement is analogous to php statement and put the  
content of “called” file into “caller”. But ruby “include” statement is  
for include one “module” that is in a “yet required file” into a class  
of the actual file.

If you intend to make reference in a class of your actual script-file to  
a module in other file, normally you have to make the following:

# called file - name hello.rb

module Called {  
def Called.Hello() # Defines Function Hello of Module Called  
puts “hello”  
end  
}

# caller file - name caller.rb

require “hello.rb”

class MyClass  
}  
include Called # Includes Module Called into MyClass  
def MyHello()  
Called.Hello() # Execute function Hello of Module “Called”  
end  
}

MyVar = MyClass.new() # Create a new MyClass Object  
MyVar.MyHello() # Execute function MyHello for MyVar, calling the  
function Called.Hello()

Javier A.

rantingrick wrote:

> Hello,
> 
> I am having trouble understanding the difference between require and  
> include. Also i need to know how to import a certain function or class  
> into my current namespace.
> 
> Now before i go any further i should explain that i am a Python  
> programmer and i understand that whilst Ruby and Python share some  
> similarities there are major differences between the languages, and  
> that is what seems to have me stumped here.
> 
> In python we use the “import” statement to bring modules, functions,  
> whatever. And the import statement can function in many ways,  
> observe…
> 
> if i want to bring all the names from the math module into my current  
> namespace without the need to qualify the names i can say…(note:  
> dir() shows the current namespace!)
> 
> > > > dir()  
> > > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’]
> > 
> > > > from math import \*  
> > > > dir()  
> > > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’, ‘acos’,  
> > > > ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’,  
> > > > ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘exp’, ‘fabs’, ‘factorial’,  
> > > > ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘hypot’, ‘isinf’, ‘isnan’, ‘ldexp’,  
> > > > ‘log’, ‘log10’, ‘log1p’, ‘modf’, ‘pi’, ‘pow’, ‘radians’, ‘sin’,  
> > > > ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘trunc’]
> > 
> > > > hypot(3,4)  
> > > > 5.0
> 
> Now if i want to just use a certain function, class, constant, i can  
> call for just those specific names like…
> 
> > > > dir()  
> > > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’]
> > 
> > > > from math import hypot, pi  
> > > > dir()  
> > > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’, ‘hypot’, ‘pi’]
> > 
> > > > pi  
> > > > 3.1415926535897931
> > 
> > > > hypot(3,4)  
> > > > 5.0
> > 
> > > >
> 
> Now if i want to bring a reference to the module in and then qualify  
> every thing within the module by perpending the  
> “module\_name.identifier” i can say…
> 
> > > > import math  
> > > > dir()  
> > > > [‘ **builtins** ’, ‘ **doc** ’, ‘ **name** ’, ‘ **package** ’, ‘math’]
> > 
> > > > math.hypot(3,4)  
> > > > 5.0
> 
> Ok, i know Ruby is different, so tell me how i do the same thing only  
> in Ruby terms. Specifically i need to bring in a function that resides  
> in a module in a different file. How do i do this?

---

<div class="post-metadata">

**Author:** ![rantingrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rantingrick](https://www.ruby-forum.com/u/rantingrick)\
**Post date:** [March 30, 2010, 5:49pm UTC](https://www.ruby-forum.com/t/require-and-include/186028/3 "2010-03-30T17:49:34Z")

</div>

On Tue, Mar 30, 2010 at 4:05 PM, rantingrick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello,
> 
> I am having trouble understanding the difference between require and  
> include. Also i need to know how to import a certain function or class  
> into my current namespace.

[http://ruby-doc.org/docs/ProgrammingRuby/html/tut\_modules.html](http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html)  
This might clear things up, but to summarise include adds methods,  
require adds modules.

> namespace without the need to qualify the names i can say…(note:  
> ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘trunc’]
> 
> > > > hypot(3,4)  
> > > > 5.0
> 
> Now if i want to just use a certain function, class, constant, i can  
> call for just those specific names like…

Not sure there is a clean way to do this.

---

<div class="post-metadata">

**Author:** ![rantingrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rantingrick](https://www.ruby-forum.com/u/rantingrick)\
**Post date:** [March 30, 2010, 6:21pm UTC](https://www.ruby-forum.com/t/require-and-include/186028/4 "2010-03-30T18:21:12Z")

</div>

On Mar 30, 10:40 am, Steve H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
(…snip…)

Thanks Steve that did it. I just used ‘require “pathtofile”’ and  
called ‘module::function()’ and voila!

Thanks everyone the responses, i learned a great deal from all of them!

---

<div class="post-metadata">

**Author:** ![rantingrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rantingrick](https://www.ruby-forum.com/u/rantingrick)\
**Post date:** [March 30, 2010, 5:47pm UTC](https://www.ruby-forum.com/t/require-and-include/186028/5 "2010-03-30T17:47:13Z")

</div>

On Mar 30, 8:03 am, rantingrick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> […]
> 
> Ok, i know Ruby is different, so tell me how i do the same thing only  
> in Ruby terms. Specifically i need to bring in a function that resides  
> in a module in a different file. How do i do this?

I can relate to your situation, since I also come from a Python  
background. It is probably good to read up on docs a bit, but you  
might also find the following examples useful for experimentation.

I think the answer to your specific question is shown by the use of  
Hello2::hello\_world2() below. Note that it is hello2.rb that actually  
“namespaces” the function (and makes it a public module function), not  
the act of calling “require” itself. This is very different from  
Python, of course.

```
::::::::::::::
foo.rb
::::::::::::::
# require without modules
require 'hello1'
hello_world1()

# require with modules in the
# other file (see hello2.rb)
require 'hello2'
Hello2::hello_world2()

# re-opening module
module Hello2
  def goodbye()
    puts 'goodbye'
  end
  module_function :goodbye
end
Hello2::goodbye()

# on-the-fly modules
hello3 = Module.new do
  def hello_world3()
    puts 'hello world 3'
  end
  module_function :hello_world3

  def obj_method()
    puts 'obj_method'
  end
end

hello3::hello_world3()
obj = 'foo'
obj.extend(hello3)
obj.obj_method()

::::::::::::::
hello1.rb
::::::::::::::
def hello_world1
    puts 'hello world 1'
end

::::::::::::::
hello2.rb
::::::::::::::
module Hello2
  def hello_world2
    puts 'hello world 2'
  end
  module_function :hello_world2
end

```

---

<div class="post-metadata">

**Author:** ![rantingrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rantingrick](https://www.ruby-forum.com/u/rantingrick)\
**Post date:** [March 30, 2010, 8:05pm UTC](https://www.ruby-forum.com/t/require-and-include/186028/6 "2010-03-30T20:05:23Z")

</div>

On Mar 30, 11:29 am, Javier A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> But the alternative is to make a “require” into the script-file to  
> import the modules, and a “include module-name” into a class definition  
> to import all of the module-methods into that class.
> 
> Javier A…

Thanks Javier! I did look into this also. It is interesting how you  
can use include inside a class definition to bring in certain  
functions. As soon as i get a good grasp on this concept i will  
probably implement it in many scripts. Thanks.

PS: i made a typo… “module::function()” should have been  
“module.function()”. Which is also weird because the two are sometimes  
interchangeable and sometimes not?

---

<div class="post-metadata">

**Author:** ![rantingrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rantingrick](https://www.ruby-forum.com/u/rantingrick)\
**Post date:** [March 30, 2010, 9:34pm UTC](https://www.ruby-forum.com/t/require-and-include/186028/7 "2010-03-30T21:34:28Z")

</div>

jbw wrote:

> This might clear things up, but to summarise include adds methods,  
> require adds modules.

That is almost completely inaccurate, I’m afraid.

‘require’ loads and executes code. For example,

```
require "foo"

```

looks for foo.rb or foo.so in the $LOAD\_PATH and then loads and executes  
it. (Actually, it first looks in $LOADED\_FEATURES to see if it has  
already been loaded, and passes if it has. Use ‘load’ to load and  
execute unconditionally)

foo.rb _might_ contain code which defines modules and/or classes, but it  
doesn’t have to. require doesn’t care, it just executes whatever is in  
there.

$ cat foo.rb  
puts “hello world”

$ irb --simple-prompt

> > require ‘foo’  
> > hello world  
> > =\> true
> 
> > require ‘foo’  
> > =\> false # has already been loaded
> 
> >

‘include’ is used to add the instance methods of a module into a class.

$ irb --simple-prompt

> > module Bar; def hello; puts “hello world”; end; end  
> > =\> nil
> 
> > class Foo; include Bar; end  
> > =\> Foo
> 
> > Foo.new.hello  
> > hello world  
> > =\> nil
> 
> >

It also works at the ‘top level’, which is probably what you’re thinking  
of.

> > include Math  
> > =\> Object
> 
> > sqrt(9) # short for Math.sqrt(9)  
> > =\> 3.0

Notice that ‘include’ takes a Module as its argument, whereas ‘require’  
takes a String (which is the filename to load).

---

<div class="post-metadata">

**Author:** ![rantingrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@rantingrick](https://www.ruby-forum.com/u/rantingrick)\
**Post date:** [March 30, 2010, 6:29pm UTC](https://www.ruby-forum.com/t/require-and-include/186028/8 "2010-03-30T18:29:35Z")

</div>

Hello,

But the alternative is to make a “require” into the script-file to  
import the modules, and a “include module-name” into a class definition  
to import all of the module-methods into that class.

Javier A…

rantingrick wrote:

> On Mar 30, 10:40ï¿½am, Steve H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> (…snip…)
> 
> Thanks Steve that did it. I just used ‘require “pathtofile”’ and  
> called ‘module::function()’ and voila!
> 
> Thanks everyone the responses, i learned a great deal from all of them!
