Jeffw Jeffw - 5 months ago 8
Ruby Question

Having trouble with this exercise

I am working on Head First Ruby. Here is my code:

class Employee
attr_reader :name

def name=(name)
end

def print_name
puts "Name: #{name}"
end
end

class SalariedEmployee < Employee
attr_reader :salary

def salary=(salary)
# code to validate and set @salary
end

def print_pay_stub
print_name
pay_for_period = (salary / 365.0) * 14
formatted_pay = format("$%.2f", pay_for_period)
puts "Pay this period: #{formatted_pay}"
end
end

class HourlyEmployee < Employee
attr_reader :hourly_wage, :hours_per_week

def hourly_wage=(hourly_wage)
# code to validate and set @hourly_wage
end

def hours_per_week=(hours_per_week)
# code to validate and set @hours_per_week
end

def print_pay_stub
print_name
pay_for_period = hourly_wage * hours_per_week * 2
formatted_pay = format("$%.2f", pay_for_period)
puts "pay This Period: #{formatted_pay}"
end
end


I cannot get this exercise to work. I get this error:

employee.rb:42:in `print_pay_stub': undefined method `*' for nil:NilClass (NoMethodError)
from employee.rb:56:in `<main>'


Could someone look over this code and tell what is going wrong?

Answer

Edit: My mistake -- either hourly_wage or hours_per_week is nil. Make sure those are set.

It looks like salary is nil -- at least, that's the only line there with an *. You need to make sure salary is set

Edit: clarification

Edit 2: correction

Comments