Created
June 24, 2016 16:26
-
-
Save michaelfairley/f463cc12d4049c3126d1bc1c8b15dba1 to your computer and use it in GitHub Desktop.
This file contains 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
module StartingPoint | |
def self.list_with_longest_word(list1, list2) | |
list1_longest = list1.map(&:length).max | |
list2_longest = list2.map(&:length).max | |
list1_longest > list2_longest ? list1 : list2 | |
end | |
end | |
# That `.map(&:length).max` "isn't DRY enough". Let's clean it up. | |
module RefactoringIUsedToDo | |
def self.list_with_longest_word(list1, list2) | |
list1_longest = longest_word_length(list1) | |
list2_longest = longest_word_length(list2) | |
list1_longest > list2_longest ? list1 : list2 | |
end | |
def self.longest_word_length(list) | |
list.map(&:length).max | |
end | |
end | |
module RefactoringIDoNow | |
def self.list_with_longest_word(list1, list2) | |
longest_word_length = lambda{ |list| list.map(&:length).max } | |
list1_longest = longest_word_length.(list1) | |
list2_longest = longest_word_length.(list2) | |
list1_longest > list2_longest ? list1 : list2 | |
end | |
end | |
list1 = %w[a b c] | |
list2 = %w[1 2 3 thisisquitelong 4 5] | |
list3 = %w[normal words] | |
[StartingPoint, RefactoringIUsedToDo, RefactoringIDoNow].each do |mod| | |
raise unless mod.list_with_longest_word(list1, list2) == list2 | |
raise unless mod.list_with_longest_word(list1, list3) == list3 | |
raise unless mod.list_with_longest_word(list2, list3) == list2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment