Jeffw Jeffw - 5 months ago 20
Ruby Question

Ruby Code. I am trying to learn Ruby and having trouble with this exercise

I am trying to learn ruby and I am working out of the book "Head First Ruby". So far I really like the book but I can not get one of the exercises to work. It is driving me crazy. I can not figure out what I have done wrong. Could someone please look over this code and tell me what is going wrong? The error I am getting is:

employee.rb:42:in

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

I can not seem to get the code up on this site in one box so here is also the gist link https://gist.github.com/jeffwolfram/cb72b4301feaf1adb55c7888ec2b3c6f#file-gistfile1-txt
Any help would be much appreciated.

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

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