separate search functionality from seller checking fucntionality according to single responsibility principle

This commit is contained in:
TimoSci
2019-12-08 12:40:40 +01:00
parent d8da439590
commit b65c3de283

View File

@@ -1,4 +1,4 @@
def person_is_seller(name) def person_is_seller?(name)
name[-1] == "m" name[-1] == "m"
end end
@@ -24,17 +24,13 @@ def search(name)
person = search_queue.shift person = search_queue.shift
# Only search this person if you haven't already searched them. # Only search this person if you haven't already searched them.
next if searched.member?(person) next if searched.member?(person)
if person_is_seller(person) return person if yield person
puts "#{person} is a mango seller!" search_queue += @graph[person]
return true
else
search_queue += @graph[person]
# Marks this person as searched # Marks this person as searched
searched.push(person) searched.push(person)
end
end end
false false
end end
search("you") search("you"){|person| person_is_seller?(person) }.tap{|person| puts "#{person} is a mango seller!" if person}