代码被困在REXML线上的无限循环中。我调试并且很奇怪,这行代码一次又一次地运行,永远。没有任何意义。
此方法来自mediawiki-gateway gem https://github.com/jpatokal/mediawiki-gateway
# Get a list of pages with matching content in given namespaces
#
# [key] Search key
# [namespaces] Array of namespace names to search (defaults to main only)
# [limit] Maximum number of hits to ask for (defaults to 500; note that Wikimedia Foundation wikis allow only 50 for normal users)
#
# Returns array of page titles (empty if no matches)
def search(key, namespaces=nil, limit=@options[:limit])
titles = []
offset = nil
in_progress = true
form_data = { 'action' => 'query',
'list' => 'search',
'srwhat' => 'text',
'srsearch' => key,
'srlimit' => limit
}
if namespaces
namespaces = [ namespaces ] unless namespaces.kind_of? Array
form_data['srnamespace'] = namespaces.map! do |ns| namespaces_by_prefix[ns] end.join('|')
end
begin
form_data['sroffset'] = offset if offset
res, offset = make_api_request(form_data, '//query-continue/search/@sroffset')
titles += REXML::XPath.match(res, "//p").map { |x| x.attributes["title"] }
end while offset
titles
end
重现的步骤如下
打开irb和代码
require 'media_wiki'
options = {
:limit => 50
}
@mw = MediaWiki::Gateway.new('http://en.wikipedia.org/w/api.php', options)
@mw.search('Strange Loop')
不幸的是,它永远不会结束。