I’ve never seen that require in any gems on the web
That’s because most of the time when you require something, it is a gem
that has been in installed in a directory that ruby searches when
you require something. You can see a list of the directories ruby
searches when you require something by doing this:
p $LOAD_PATH
If one of those directories isn’t your current directory, and you want
to require a file in your current directory, then your require has to
specify the path(absolute or relative) to the file.
require ‘./your_file.rb’
Or you can set an environment variable to make ruby look in
the specified directories, or you can add directories to $LOAD_PATH.
That’s because most of the time when you require something, it is a gem,
and gems are installed in specific directories that ruby searches when
you require something. You can see a list of the directories ruby
searches when you require something by doing this:
p $LOAD_PATH
7Stud was very clear…
The gems are installed to a folder/directory that is in your environment
path.
So you won’t see this in a gem, unless someone is unaware of what they
are
doing, and it would also require you to run that specific gem in some
specific folder or directory.
And then when I publish my gem and users use my gem, things will just work
since the gem is installed by default at a path that ruby will look in?
How then do gem developers modify their path to have this work?
They don’t. They usually end up using something that 7Stud also
mentioned.
Which is to say require_relative or they add a relative path to the
$PATH
variable in some way.
So they don’t modify their path, they use the environment that will be
given, and take advantage of this.
You can also use the file itself… you will see many different examples
of
this in different gems. For example, in Rails Admin,
spec/spec_helper.rb
you will see require File.expand_path(‘…/dummy_app/config/environment’, FILE)
This takes the relative location of that file and load it that way, and
so
will not require it to know exactly where it was installed, but it can
rely
on its own project structure.
In IRB go ahead and do that… >> File.expand_path(‘.’) you will see
what I
mean. From that piece of information, build your path as you need to
require things for your project.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.