the12 the12 - 1 year ago 86
Ruby Question

Why is the "./" (dot + forward slash) needed in Ruby files when using the require keyword?

In Windows, when attempting to place Ruby dependency files into a Ruby file, why is the "./" (dot + forward slash) necessary when typing out the file directory? Oddly enough it is only needed when using the

keyword and not the

module.rb (the dependency)

module SomeModule
def someMethod
puts "hello"


require "./module.rb"
#require "module.rb" does not work

class Animal
include SomeModule

class Person
include SomeModule

animal =

person =

#irb --> need to also write require " ./method.rb" to call it

Answer Source

It is only needed when using the require keyword and not the load keyword. Why?

load, require and require_relative are all methods which take a filename as an argument. They need to locate the filename passed an an argument which they do by checking the LOAD PATH which can be accessed using the global variable $LOAD_PATH or $:

Both load and require_relative act as if the current directory (designated by the .) is part of the LOAD_PATH so they are able to locate the file within the LOAD PATH when ruby searches it.

However when require is used, it does not add the current directory to the LOAD PATH so once ruby searches it, it doesn't find the file. This is why you have to explicitly tell ruby about the current_directory and how to locate the file using ./ which is just a path to the folder from the current directory.

If you don't want to add ./, you have to add the current directory to your Load Path

$: << '.'
require 'module.rb'

This will work as the current directory is now in the load path which is where ruby will search for the file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download