I have a Ruby code with different classes in a few files. In one file, I start the execution. This file requires my other files.
- Is this a good way to start a ruby code?
- When I run the code from a symbolic link, for example
DIR2/MyRubyCodeis a link to the main fileDIR1/MyRubyCode.rb, then my requires will fail. I solved the problem by adding the pathDIR1to$LOAD_PATHbefore therequire, but I think there would be much better ways to do it. Do you have any suggestions about that?
If you want to check if a Ruby file is being ‘
require‘ed or executed with ‘ruby MyRubyCode.rb‘, check the__FILE__constant.As far as the require/$LOAD_PATH issue, you could always use the relative path in the require statement. For example:
Which would include the
foo_class.rbandbar_module.rbfiles in the same directory asMyRubyCode.rb.