Head First Ruby

Head First Ruby

Author:Jay McGavren
Language: eng
Format: epub
Publisher: O’Reilly Media, Inc.
Published: 2015-08-20T16:00:00+00:00


Success! Thanks to self and the dot operator, it’s now clear to Ruby (and everyone else) that we’re making calls to the attribute writer methods, not assigning to variables.

And since we’re going through the accessor methods, that means the validation works, without any duplicated code!

When “self” is optional

Right now, our print_pay_stub method accesses the @name and @salary instance variables directly:

class Employee

def print_pay_stub

puts "Name: #{@name}"

pay_for_period = (@salary / 365.0) * 14

formatted_pay = format("$%.2f", pay_for_period)

puts "Pay This Period: #{formatted_pay}"

end



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.