# I need some help plz with my ruby project

**URL:** <https://www.ruby-forum.com/t/i-need-some-help-plz-with-my-ruby-project/116974>\
**Category:** Ruby\
**Created:** [October 11, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/i-need-some-help-plz-with-my-ruby-project/116974 "2007-10-11T20:10:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 11, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/i-need-some-help-plz-with-my-ruby-project/116974/1 "2007-10-11T20:10:48Z")

</div>

First, i need to extend the student class, and its to be enumerable by  
courses. Then, show off the new functionality supported by this  
change by writing an expression that returns a list of all of the  
grades a student has earned in the courses he or she has completed for  
the major.

## here is what I got so far…what am i missing

irb

# This class initializes a new student with ID,

# and shows a list of completed courses,

# and also shows a list of grades from completed courses.

class Student

include Enumerable

attr\_reader :name, :id

# Initializing the students name and ID.

# Name = Student’s name.

# ID = Student’s ID.

# Initializing courses to equal a new hash.

def initialize(name, id)  
@name = name  
@id = id  
@courses = {}  
end

# Adding a course to the set of those completed.

def add\_course(code, name, grade)  
@courses[code] = [code, name, grade]  
end

# Retrieving the grade for a particular course.

def get\_grade(code)  
(c=@courses[code]) && c[Grade]  
end

# Changing the grade for a course completed.

def set\_grade(code, grade)  
@courses[code][Grade] = grade  
end

# Providing a list of all of the courses completed.

def list\_courses  
@courses.values  
end

def each\_grade  
@courses.each { |course| yeild course }  
end  
end

s1 = Student.new(“Eric”, “00101”)  
s1.add\_course(“22c:021”, “Computer Science I”, “B-”)  
s1.each\_grade.collect { |course| do course[2] }  
puts s1

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 11, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/i-need-some-help-plz-with-my-ruby-project/116974/2 "2007-10-11T21:04:49Z")

</div>

well, to start I noticed a few syntax errors, yield is misspelled, and  
in the line s1.each\_grade.collect { |course| do course[2] } I don’t  
know what you were trying to do with ‘do course[2]’ maybe yield  
course[2] and your get\_grade method has c[Grade] but I don’t know what  
Grade is, so maybe it should be  
def get\_grade(code, Grade)  
(c=@courses[code]) && c[Grade]  
end

maybe that might help a little

~Jeremy

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 11, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/i-need-some-help-plz-with-my-ruby-project/116974/3 "2007-10-11T21:14:50Z")

</div>

oh, and also, take out the collect on s1.each\_grade and try just  
passing a block to each\_grade so  
irb(main):009:0\> s1.each\_grade { |course| puts course[2] }  
nil  
=\> {“22c:021”=\>[“22c:021”, “Computer Science I”, “B-”]}

HTH

~Jeremy

On Oct 11, 12:04 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 15, 2007, 5:38am UTC](https://www.ruby-forum.com/t/i-need-some-help-plz-with-my-ruby-project/116974/4 "2007-10-15T05:38:06Z")

</div>

class Student  
attr\_accessor :courses  
def initialize(hash = {})  
@name = hash[:name]  
@id = hash[:id]  
@courses = []

```
# if you need, write on some validations here.

```

end  
end

class Course  
def initialize(hash = {})  
@code = hash[:name]  
@name = hash[:id]  
@grade = hash[:grade]

```
# if you need, write on some validations here.

```

end  
end

s1 = Student.new :name =\> “Eric”, :code =\> “00101”  
s1.courses \<\< Course.new(:code =\> “22c:021”,  
:name =\> “Computer Science I”,  
:grade =\>“B-”)  
p s1.courses.map {|c| c[:grade] }
