Requiring Files Notes Flashcards

You may prefer our related Brainscape-certified 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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 &laquo_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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly