Unexpected Message

I hate asking quetions but here goes…
When I run rspec for course, everything goes according to plan. BUt when I try to run enroll from student spec, i run into this problem…it’s having issues accessing course’s attributes for some reason. Atfirst I thought it was something to do with the hash itself, but then I .tried to output the name, course, and credits in a puts string and ran into the same error (only it said :name with (no args) instead of department)…but when I ran it from " bundle exec rspec spec/course_spec.rb" the message printed out perfectly.

What is going on here?

Failure/Error: student.enroll(new_course)
Double :new_course received unexpected message :department with (no args)
# ./lib/student.rb:42:in enroll' # ./spec/student_spec.rb:36:inblock (3 levels) in <top (required)>’

  1. Student#enroll updates the course’s list of students
    Failure/Error: student.enroll(new_course)
    Double :new_course received unexpected message :department with (no args)

    ./lib/student.rb:42:in `enroll’

    ./spec/student_spec.rb:36:in `block (3 levels) in <top (required)>’

  2. Student#enroll ignores attempts to re-enroll into a course
    Failure/Error: student.enroll(new_course)
    Double :new_course received unexpected message :department with (no args)

    ./lib/student.rb:42:in `enroll’

    ./spec/student_spec.rb:36:in `block (3 levels) in <top (required)>’

  3. Student#course_load returns a hash of department names pointing to # of credits
    Failure/Error: expect(student.course_load).to eq({

    expected: {“CS”=>8, “Theatre”=>2}
    got: {}

    (compared using ==)

    @@ -1,3 +1 @@
    -“CS” => 8,
    -“Theatre” => 2,

    ./spec/student_spec.rb:61:in `block (3 levels) in <top (required)>’

Here is my enroll

def enroll(new_course)
if self.courses.include?(new_course) == true
puts “student already exists”
new_course.students << self
self.courses << new_course
# puts “Course name: #{new_course.name} Department: #{new_course.department} Credits: #{new_course.credits}”
self.course_load.key?(new_course.department) ? self.course_load[new_course.department] += new_course.credits : self.course_load[new_course.department] = new_course.credits
# puts self.course_load.inspect

And here is add student

def add_student(student)
student.enroll(self) unless @students.include?(students)

Here is what outputs when I test with additional puts statements

FWe are within the else statement.
Course now contains [#<RSpec::Mocks::Double:0x3fddd0c80224 @name=:student>, #<Student:0x00007fbba1900830 @first_name=“Johnny”, @last_name=“Rocket”, @courses=[#<RSpec::Mocks::Double:0x3fddd0c80300 @name=:old_course>], @course_load={}>]
THis student now has the following courses: [#<RSpec::Mocks::Double:0x3fddd0c80300 @name=:old_course>, #<RSpec::Mocks::Double:0x3fddd0c80120 @name=:new_course>]

Hey Aaron,

I think the issue might be stemming from the fact that you have course_load included in your Student#enroll.

Did you create a Student#course_load method?

Also, the enroll method should just ignore an attempt to add a student if the student is already enrolled in the course. I didn’t use any puts statements, the method simply doesn’t re-add the student. When you get to the extras part you will alter the enroll to raise an error.

Hope this helps.


I have a course_load method that returns the instance variable, which was initialized to a blank hash.
i added the puts for testing purposes to make sure the checks were being made.
When a student enrolls i would suspect a new hash entry to be added in enroll.
Was I mistaken?
If so, the instructions need rephrasing.
My puts statements proved that when running course (not student) the hash DOES get updated…but when running student it fails miserably.

Aarons-MacBook-Air:appacademy-online-students-courses-103ccbf70eb0 aarongoddard$ bundle exec rspec spec/course_spec.rb
…course added, course load is: {“CS”=>4}

Finished in 0.00809 seconds (files took 0.1063 seconds to load)
4 examples, 0 failures

As you can see, the hash populates and is accessible within enroll…but ONLY if enroll gets called from course’s add_student method

When I comment out this line
self.course_load.key?(new_course.department) ? @course_load[new_course.department] += new_course.credits : @course_load[new_course.department] = new_course.credits

Everything BUT the hash works (because it doesn’t even try)
For some inexplicable reason, this line negates the ability of two previous lines to function (adding course to list of courses, for example).

Hmm. Hard to tell exactly whats going on…

#course_load simply returns a hash with departments as keys and the total credits (of that dept) as values.

You shouldn’t need to initialize a hash for either classes.

Also, your Course#add_student method doesn’t need the unless statement.
The Student#enroll method should already take care of checking if the student is already enrolled.

student.rb should pass all specs before you move onto course.rb.

But I can’t RETURN a hash with departments and credits if it’s empty…
It’s empty until the student enrolls in a few classes, right?
The instructions gave me the impression that I needed to add hash entries every time a . student enrolled successfully in a class.
I changed it so that course_load creates a new hash using an each loop on @courses, and that worked.
The instructions say that course_load should RETURN a hash, not that it should create one.

Correct, just return a hash, no need to create/initialize one.

Hope that solves everything.

But wait, I DID have to create one…I had to create one in course_load!!!

hash = {}
@courses.each {|course| hash.key?(course.department) ? hash[course.department] += course.credits : hash[course.department] = course.credits}

You can’t return that which doesn’t exist.

Yes, but its not being saved in the instances. It creates and returns, then its gone.

Make sure you set up the keys to default to zero.

^^^^ edit: Actually, I think your method works, but you could skip the ternary op and simply default keys to zero.

hash = Hash.new(0)
courses.each { |course| result[course.department] += course.credits }