Students& Courses question

Hi, I have questions on the solution below 1)who does self referring to? 1) why do we need to set days and time_block to nil?

Thanks!

def add_student(student)
student.enroll(self)
end

class Course
attr_reader :name, :department, :credits, :days, :time_block, :students

def initialize (name, department, credits, days=nil, time_block=nil)
@name, @department, @credits, @days, @time_block =
name, department, credits, days, time_block
@students = []
end

1 Like

1.) ‘self’ in an instance method is always referring to the instance of the class. In this case ‘self’ is referring to the instance of the Student class.

2.) The specs expects days and time_block to be optional arguments. Ruby always expects every argument to be filled with a value when the method is invoked, otherwise it will throw an error. In order to make an argument an optional input, we need to set the argument to a default value. In this case, we are setting days and time_block to ‘nil’ as the default value, if no value is given for these arguments.

----Thank you Sally!