what does require_relative do?
it is a method we can use to specify a path to another ruby file. As it’s name suggests, we need to specify a path that is relative to our current location.
how do you use require_relative?
project_root
├── pet_hotel.rb
├── cat.rb
└── other_animals
└── dog.rbproject_root/pet_hotel.rb
# Let's require the last two files, by specifying their path's relative to this pet_hotel.rb file require_relative "./cat.rb" require_relative "./other_animals/dog.rb"
class PetHotel
def initialize(name)
@name = name
@guests = []
enddef check_in(guest)
@guests «_space;guest
end
end
hotel = PetHotel.new(“Animal Inn”)
cat = Cat.new("Sennacy")
dog = Dog.new("Fido")hotel. check_in(cat)
hotel. check_in(dog)
when do you use the plain require over the require_relative?
As a rule of thumb, we’ll use the plain require where gems are involved