Need help with converting require_relative to require only!

Just some quick background, I did some coding-challenge for a junior ruby position while I’m happy with the results. Digging in other people ruby code I notice very few people uses the require_relative and only using require for requiring modules.

I want to avoid these kind require statements.

How can I achieve this?

Here is the full source code GitHub - guledali/cart

To avoid that you would need your $LOAD_PATH global variable to include the path were those files are. Usually bundler takes care of that adding the lib folder of a gem to the $LOAD_PATH but I think you can just add new paths to that variable when needed.

I’m not able to require the top module(shopping_cart) in the irb session. All the other ones are working

Requiring everything inside the cart folder is working?

I think require returns false if the file has been required before.

I have actually included the source code GitHub - guledali/cart

If someone can pin point the changes I need to make in order to make require work?

I want to make these following steps to work

But rather the user typing this irb session

require_relative("shopping_cart.rb")

Should be enough with this,

require("shopping_cart")

Make sure to read the documentation. It describes how require will search for files to load.

https://rubyapi.org/3.0/o/kernel#method-i-require

If the file name you pass starts with ./ or ../ then require will treat it like a relative path based on the current working directory.

Otherwise like @arielj said you will have to make sure the directory containing the file you want to load is in the load path. There’s a lot of ways to accomplish this. Bundler is one. Zeitwerk is another. You could also just manually push directories onto the $LOAD_PATH global variable. That’s what people did before Zeitwerk and Bundler came around.

Taking a look at your code, if you want to change the require_relative statements to require without relative paths, you will need to get your lib/ directory into the load path, using one of the tools I described.