-
-
Save practicingruby/607921 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Extract Class (http://refactoring.com/catalog/extractClass.html) | |
class Person | |
attr_accessor :name, :office_area_code, :office_number | |
def telephone_number | |
[office_area_code, office_number].join('-') | |
end | |
end | |
# Refactored | |
class Person | |
attr_accessor :name | |
def office_telephone | |
@office_telephone ||= TelephoneNumber.new | |
end | |
def telephone_number | |
office_telephone.telephone_number | |
end | |
class TelephoneNumber | |
attr_accessor :area_code, :number | |
def telephone_number | |
[area_code.to_s, number].join('-') | |
end | |
end | |
end | |
-- | |
p = Person.new | |
p.name = "Jordan" | |
p.office_telephone.area_code = 203 | |
p.office_telephone.number = "555-5555" | |
p.telephone_number #=> "203-555-5555" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment