Home > Blockchain >  Using require/require_relative method in def initializer is ok?
Using require/require_relative method in def initializer is ok?

Time:10-29

I did not find the correct place to use the require method in any documentation. I understand that it is desirable to use the require method at the very beginning of the script, but is it possible and how correct is it to use the require method in the initializer class constructor?

Usually i make like this require 'my_file'

how good is it to do the option below

class MyClass
def initialize
require 'my_file'
end
end

Read require method documentation

CodePudding user response:

I did not find the correct place to use the require method in any documentation.

The "correct" place depends on your requirements and also on your coding style.

is it possible and how correct is it to use the require method in the initializer class constructor?

It is possible. Whether it is "correct" or not depends on your definition of "correct" and it depends on what you want to do.

require only does three things:

  1. Search for a file in the load path.
  2. Check whether the file was already run.
  3. If it wasn't, run the file.

That's all require does.

If you require a file in the initializer, require will only run when you call the initializer (which is usually when you instantiate a new object).

If you need some functionality from the file before you instantiate an object, you are out of luck, since the require will not have run.

Also, you will run require every time you instantiate an object, but since require only runs the file once, that will simply be a waste of time, since it will still search for the file and check whether it was already run every time.

Lexically nesting a call to require like this can also be confusing. Someone who is not familiar with Ruby may assume that by requireing myfile inside the initialize method, the definitions inside my_file might be scoped purely to the initialize method, but that is obviously not the case: require simply runs the file, it does not perform any scoping. The definitions inside my_file will be defined wherever my_file says they are.

In the same vein, someone who is reviewing your code might assume that you think that this creates some sort of scoping and might now wonder whether your code has some sort of bug or (worse) security leak based on that assumption.

And note that "someone who is reviewing your code" might very well be yourself in two years, when you have no idea anymore why you wrote the code this way.

CodePudding user response:

You can put it on top of the file before the class declaration. So you have:

require 'my_file'

class MyClass
 def initialize
 end
end

CodePudding user response:

It's possible, it will work correctly, but it's highly unusual. I guarantee this will raise questions in code review.

Normally, requires are placed on top of the file.

CodePudding user response:

Declaring the require "./file_name" is not recommended to use in the constructor method as it is often used to create the instance variable and particularly: if you are going to use an instance or global variable from that required file class as a parameter to the initialize (constructor) method of the current class, it may end up with an error.

You may use these ways to require the file at the top of the class:

require 'my_file'

class MyClass
    def initialize
       puts "this is a constructor"
    end
end
   

or you may also require it just before calling your class is highly recommended to use require

class MyClass
    def initialize
       puts "this is a constructor"
    end
end

require 'my_file'
object = MyClass.new
object.some_method

This allows you to use all the instance, class, method, object...etc.. in your current class.

  • Related