Chapter 4. Initializing Instances: Off to a Great Start

Right now, your class is a time bomb. Every instance you create starts out as a clean slate. If you call certain instance methods before adding data, an error will be raised that will bring your whole program to a screeching halt.
In this chapter, we’re going to show you a couple of ways to create objects that are safe to use right away. We’ll start with the initialize method, which lets you pass in a bunch of arguments to set up an object’s data at the time you create it. Then we’ll show you how to write class methods, which you can use to create and set up an object even more easily.
Payroll at Chargemore
You’ve been tasked with creating a payroll system for Chargemore, a new chain of department stores. They need a system that will print pay stubs for their employees.
Chargemore employees are paid for two-week pay periods. Some employees are paid a two-week portion of their annual salary, and some are paid for the number of hours they work within the two-week period. For starters, though, we’re just going to focus on the salaried employees.
A pay stub needs to include the following information:
The employee name
The amount of pay an employee received during a two-week pay period
So...here’s what the system will need to know for each employee:
Employee name
Employee salary
And here’s what it will need to do:
Calculate and print pay for ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access