Help newbie plz! Thnx for help

#problem is in the end
#when i put number instead of “lol” its OK? but when i put string value - its error

class Employee
attr_reader :name

def name= (name)
if name == “”
raise “Invalid name!!!”
end
@name = name
end

def initialize (name = “Nobody”)
self.name= name
end

def show_name
puts “Name: #{@name}”
end
end

class EmployedWorker<Employee
attr_reader :salary

def salary= (salary)
if salary<0
raise “Invalid salary!!!”
end
@salary = salary
end

def initialize(salary = 0.0)
super(name)
self.salary = salary
end

def self.sonOfTheBoss(name)
EmployedWorker.new(name)
end

def show_salary
show_name
salary = @salary/365.0*14
formatted_salary = format("%.2f",salary)
puts “Salary for 14 days: $#{formatted_salary}”
end
end

me = EmployedWorker.new
me .name = "me "
me .salary = 1000000
me .show_salary

#HERE

loh = EmployedWorker.sonOfTheBoss(“lol”)

#HERE

loh.salary = 100
loh.show_salary

loop {sleep 10}

Well, yes. Your constructor for EmployedWorker takes a single argument, which is its salary. But your sonOfTheBoss function has called its variable name. Look at your EmployedWorker constructor:

class EmployedWorker<Employee

  def initialize(salary = 0.0)
    super(name)                    # what is 'name' supposed to be???
    self.salary = salary
  end
end

You tried to pass name to the parent class, but name is not defined. You probably wanted to write something like:

class EmployedWorker<Employee

  def initialize(name = "no name", salary = 0.0)
    super(name)
    self.salary = salary
  end
end

You can then create your instances with values:

me = EmployedWorker.new("me", 1000000)
me.show_salary

Thanks a lot!
I thought when I writed super(name) it will inherit all of initialize from superclass, but it didn’t

The way to think of it is that super is simply a method call, except that it calls the method of the same name in the parent class.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs