When using class files in Ruby, do you put the 'requires' statements at the top of the file, or inside the class definition?
At the top of the file, the majority (but not all) languages handle imports this way. I find it much cleaner and easier to handle them this way.
I think it only makes sense this way really... like you get mid way in a file then:
class Foo
def initialize(init_value)
@instance_var = init_value
# some 500 lines of code later....
end
end
class Bar
# oh look i need an import now!
require 'breakpoint'
as you can see, it would be very hard to track them. Not to mention if you wanted to use the imported functions earlier in your code, you would probably have to backtrack and include it again because the other import would be specific to that class. Importing the same files would create a lot of overhead during runtime as well.
Technically, it doesn't really matter. require
is just a normal method call. 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.
I can see a possible reason for not putting a require
at the top of the file: where it's expensive to load and not always executed. One case that occurs to me is where, for example, code and its tests are in the same file, which is something I like to do from time to time for small library code in particular. Then I can run the file from my editor and the tests run. In this case when the file is require
d in from elsewhere, I don't want test/unit
to be loaded.
Something a little like this:
def some_useful_library_function()
return 1
end
if __FILE__ == $0
require 'test/unit'
class TestUsefulThing < Test::Unit::TestCase
def test_it_returns_1
assert_equal 1, some_useful_library_function()
end
end
end
It doesn't really matter where you put them, but if you put them inside a class
or module
expression, then it looks like you are importing whatever is in the require
d file into the class's namespace, which is not true: everything ends up in the global namespace (or whatever namespaces are defined in the library).
So, better put them at the top to avoid any confusion.