Requiring Files Notes Flashcards
1
Q
what does require_relative do?
A
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.
2
Q
how do you use require_relative? project_root ├── pet_hotel.rb ├── cat.rb └── other_animals └── dog.rb
A
project_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 = [] end
def 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)
3
Q
when do you use the plain require over the require_relative?
A
As a rule of thumb, we’ll use the plain require where gems are involved