June 2017
Beginner
330 pages
7h 30m
English
Here we have created a new class called Mailer that has a single method that will send out an email. The email method takes an argument where we can pass the invoice details to it:
class Invoice def initialize(customer:, state:, total:) @customer = customer @state = state @total = total end def details "Customer: #{@customer}, Total: #{@total}" end def sales_tax case @state when 'AZ' then 5.5 when 'TX' then 3.2 when 'CA' then 8.7 end endendclass Mailer def self.email(content) puts "Emailing..." puts content endendinvoice = Invoice.new(customer: "Google", state: "AZ", total: 100)puts invoice.sales_taxMailer.email(invoice.details)
If we run this code, we'll see this is working properly:
5.5Emailing invoice...Customer: Google, ...
Read now
Unlock full access