From b65c3de28399daed7eeacbcda821485475e60532 Mon Sep 17 00:00:00 2001 From: TimoSci Date: Sun, 8 Dec 2019 12:40:40 +0100 Subject: [PATCH] separate search functionality from seller checking fucntionality according to single responsibility principle --- .../ruby/01_breadth-first_search.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/06_breadth-first_search/ruby/01_breadth-first_search.rb b/06_breadth-first_search/ruby/01_breadth-first_search.rb index cb21b9f..a47e0d0 100644 --- a/06_breadth-first_search/ruby/01_breadth-first_search.rb +++ b/06_breadth-first_search/ruby/01_breadth-first_search.rb @@ -1,4 +1,4 @@ -def person_is_seller(name) +def person_is_seller?(name) name[-1] == "m" end @@ -24,17 +24,13 @@ def search(name) person = search_queue.shift # Only search this person if you haven't already searched them. next if searched.member?(person) - if person_is_seller(person) - puts "#{person} is a mango seller!" - return true - else - search_queue += @graph[person] + return person if yield person + search_queue += @graph[person] # Marks this person as searched - searched.push(person) - end + searched.push(person) end false end -search("you") +search("you"){|person| person_is_seller?(person) }.tap{|person| puts "#{person} is a mango seller!" if person}