質問
require 'net/http';
require 'uri';
require 'rexml/document';
require 'sqlite3';
def download_torrent(episode_id, torrent_url, limit = 10)
# Check to make sure we haven't been trapped in an infinite loop
if limit == 0 then
puts "Too much redirection, skipping #{episode_id}";
return true;
else
# Convert the URL of the torrent into a URI usable by Net:HTTP
torrent_uri = URI.parse(torrent_url);
# Open a connection to the torrent URL
Net::HTTP.get_response(torrent_uri) { |http|
# Check to see if we were able to connect to the torrent URL
case http
# We connected just fine
when Net::HTTPSuccess then
# Create a torrent file to store the data in
File.open("#{episode_id}.torrent", 'wb') { |torrent_file|
# Write the torrent data to the torrent file
torrent_file.write(http.body);
# Close the torrent file
torrent_file.close
# Check to see if we've download a 'locked' torrent file (html) instead of a regular torrent (.torrent)
if(File.exists?('download.torrent.html'))
# Delete the html file
File.delete('download_torrent.html');
return false;
else
return true;
end
}
when Net::HTTPRedirection then
download_torrent(episode_id, http['location'], limit - 1);
end
}
end
end
私の機能は、ブールの意味で「真」を返しません。戻り続けます <Net::HTTPFound:0x3186c60>
私の「この関数はtrueを返します」条件付きでfalseに評価します。なぜこの関数が最初の返品ステートメントにヒットしたときに終了しないのですか(私が使用した他のすべての言語と同様)
私はこれが完全にルビーの初心者(私は韻を踏んだ!)の質問であることを知っていますが、私は助けに感謝しています。
解決
どうやら、 Net::HTTP.get_response
合格しました Net::HTTPFound
インスタンスとして http
内側ブロックへのパラメーター。したがって、 return
声明に到達することはありませんでした download_torrent
メソッドは最後のオブジェクトを「スタック上」に返しました。 Net::HTTP.get_response
.
その説明が少し曖昧な場合は、短い例です。と return true
部、 do_it
メソッドが返されます true
. 。それなしで、 do_it
メソッドは、返されたオブジェクトを返します do_that
.
def do_it
do_that {|param|
# return true;
}
end
経験はほとんどありません net/http
パッケージ、おそらくドキュメントを読んで処理する必要があります Net::HTTPFound
あなたの応答 case
どういうわけかオペレーター。
個人的には、これは常にWebページのコンテンツを取得する上で私のために機能しました。 Net::HTTP.get(URI.parse(url))
. 。コードブロックがなくてもシンプルで、単にコンテンツを文字列として返すだけです。
所属していません StackOverflow