I don't get this, when I try to run below code, I am getting
employee.rb:55:in `hourly_wage=': stack level too deep (SystemStackError)
class HourlyEmployee < Employee
attr_reader :hourly_wage, :hours_per_week
def hourly_wage=(hourly_wage)
self.hourly_wage = hourly_wage
end
def hours_per_week=(hours_per_week)
self.hours_per_week = 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 for this period: #{formatted_pay}"
end
end
Isn't this basically the same in Java where I use the this keyword in the setter method?
If I replaced the def with then everything works fine.
def hourly_wage=(hourly_wage)
@hourly_wage = hourly_wage
end
Sorry, just started picking up Ruby
def hourly_wage=anddef hours_per_week=. You can simly changeattr_reader :hourly_wage, :hours_per_weektoattr_accessor :hourly_wage, :hours_per_week- sig