The Mailbox Class

Exercise 11.1

In a new file mailbox-1.rb Write a class that has a name and emails attribute. Make it so that the these attributes can be populated through the initialize method (name being a string, and emails being an array of Email instances).

The following code

  1. class Email
  2. # your class from the last exercise
  3. end
  4. class Mailbox
  5. # fill in this class body
  6. end
  7. emails = [
  8. Email.new("Homework this week", { :date => "2014-12-01", :from => "Ferdous" }),
  9. Email.new("Keep on coding! :)", { :date => "2014-12-01", :from => "Dajana" }),
  10. Email.new("Re: Homework this week", { :date => "2014-12-02", :from => "Ariane" })
  11. ]
  12. mailbox = Mailbox.new("Ruby Study Group", emails)
  13. mailbox.emails.each do |email|
  14. puts "Date: #{email.date}"
  15. puts "From: #{email.from}"
  16. puts "Subject: #{email.subject}"
  17. puts
  18. end

should then output the following:

  1. Date: 2014-12-01
  2. From: Ferdous
  3. Subject: Homework this week
  4. Date: 2014-12-01
  5. From: Dajana
  6. Subject: Keep on coding! :)
  7. Date: 2014-12-02
  8. From: Arianne
  9. Subject: Re: Homework this week

Show solution