class ProjectEulerCli::ArchiveSearcher
Handles searching the problems
Attributes
results[R]
Array of IDs corresponding to the problems found in last search
searching[RW]
Tracks whether there is an active search
Public Class Methods
new()
click to toggle source
# File lib/project_euler_cli/archive_searcher.rb, line 12 def initialize @results = [] @searching = false @initial_search = true end
Public Instance Methods
load_keywords()
click to toggle source
Loads the problem numbers and titles for every page that is not loaded.
# File lib/project_euler_cli/archive_searcher.rb, line 19 def load_keywords puts "updating keywords..." 0.upto(Page.total) { |page| load_page(page) } end
search(search_string)
click to toggle source
Performs a simple search of the problems. It accepts multiple terms and recognizes quoted phrases. Results will contain all of the search terms.
-
terms
- String of search terms
# File lib/project_euler_cli/archive_searcher.rb, line 29 def search(search_string) load_keywords if Page.visited != (0..Page.total).to_a puts "searching..." @searching = true search_string.downcase! terms = search_string.scan(/"[^"]*"/) terms.each { |term| search_string.slice!(term) } terms.collect! { |term| term.gsub("\"", '') } terms += search_string.split(' ') @results = (1..Problem.total).select do |i| terms.all? { |term| Problem[i].title.downcase.include?(term) } end end