Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

object

ruby

When using class files in Ruby, do you put the 'requires' statements at the top of the file, or inside the class definition?

like image 851
Ash Avatar asked Mar 03 '09 05:03

Ash


People also ask

What is require in Ruby?

In Ruby, the require method is used to load another file and execute all its statements. This serves to import all class and method definitions in the file.

What is the difference between require and include in Ruby?

Use the include Method in Ruby Unlike require , which loads an entire file's code, include takes a module name and makes all its methods available to other classes or modules.


1 Answers

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.

like image 105
Chuck Avatar answered Sep 30 '22 18:09

Chuck