Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # @author Mark Seymour (mark.seymour.ns@gmail.com)
- require 'date'
- class Person
- attr_accessor :first_name
- attr_accessor :last_name
- attr_accessor :birthday
- # @param [String] The person's first name
- # @param [String] The person's last name
- # @param [Date] The person's birthday
- # @raise [TypeError] Raises TypeError if birthday is not a Date object.
- def initialize first_name, last_name, birthday
- @first_name = first_name.to_s.capitalize
- @last_name = last_name.to_s.capitalize
- # Only set birthday if it is a Date object. Otherwise, raise a TypeError
- @birthday = (birthday.is_a?(Date) ? birthday : raise(TypeError, "Expected 'Date'"))
- end
- # Determines whether the person's birthday is today or not.
- # @todo Make it compatible with leap years
- # @return [Boolean] True if the person's birthday is today, otherwise false.
- def birthday?
- today = Date.today
- @birthday.mon == today.mon && @birthday.day == today.day ? true : false
- end
- # Combines the first_name and last_name attributes into one.
- # @param [Boolean] (false) Formats the name using the "Lastname, Firstname" convention.
- # @return [String] The person's name
- def fullname reversed = false
- reversed ? "#{@last_name}, #{@first_name}" : "#{@first_name} #{@last_name}"
- end
- end
- # Note that I am using -1 for the year since I do not know Sean's birth year. ;)
- seanmorrow = Person.new("Sean", "Morrow", Date.new(-1,03,17))
- if seanmorrow.birthday?
- puts "Happy Birthday, #{seanmorrow.fullname}!"
- else
- puts "It's not #{seanmorrow.fullname}'s birthday... yet."
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement