Ruby / get Class Set

What is wrong with that set/get?

class Pupil
  def name
    @name
  end

  def name=(name)
    @name = name
  end

  def age
    @age
  end

  def age=(age)
    @age
  end
end

Further, if there were a child class with three arguments, name, age, gender, the recruitment method would only be received by the child for sex only. Can you show the set / get method and initialize in the child class.

+5
source share
2 answers
def age=(age)
    @age
  end

it should be

  def age=(age)
    @age = age
  end

You can also make your code beautiful by replacing get / set with attr_accessor, which getter / setter itself provides

 class Pupil
   attr_accessor :age,:name
 end
+13
source

You forgot to install @age = age.

+1
source

All Articles