Gurmukh Singh Gurmukh Singh - 2 months ago 7
Ruby Question

unexpected ',' syntax error ruby

I'm learning ruby and have looked the internet to see whats wrong with this. I have tried adding spacing and removing it between the variables I'm passing but i keep getting this error:

25: syntax error, unexpected ',', expecting ')'
student1.grades =(60,70,80)


Here is the code:

class Student
attr_accessor :name, :age

def initialize(name,age)
@name = name
@age = age
end

def grades(math,english,science)
@math = math
@english = english
@science = science
average_grade = (math.to_i + english.to_i + science.to_i) / 3
return average_grade
end

def to_s
puts "Name = #{name}"
puts "Age = #{age}"
puts self.grades
end
end

student1 = Student.new("Tom","23")
student1.grades = (60,70,80)
puts student1

Answer

grades receives three parameters. You don't do that with assignment. So change

student1.grades = (60,70,80)

to

student1.grades(60,70,80)