2013-07-29 60 views
1

基於this我試圖用ruby向DOI應用程序發送一個DOI(文檔對象標識符),它應該返回書目信息,從命令行中進行以下調用WinXP中運行的Ruby 1.9.3-P194:用命令行的ruby打開解析DOI的網址

c:\Ruby193\bin\ruby.exe < rtest.txt 

該文件rtest.txt包含

open("http://dx.doi.org/10.1038/nrd842","Accept" => "text/bibliography; style=bibtex"){|f| f.each {|line| print line}} 

相反,它不會太遠,返回如下:

-:1:in `initialize': Invalid argument - http://dx.doi.org/10.1038/nrd842 (Errno::EINVAL) 
     from -:1:in `open' 
     from -:1:in `<main>' 

我可以調用ruby,如圖所示執行其他任務,如刪除文件(gasp)。我想知道什麼可能是錯誤的原因。

回答

1

看來你必須首先需要一個庫,如「開放-URI」就像下面這個例子:

[email protected]:~$ irb 
irb(main):001:0> require 'open-uri' 
=> true 
irb(main):002:0> open("http://dx.doi.org/10.1038/nrd842","Accept" =>"text/bibliography; style=bibtex"){|f| f.each {|line| print line}} 
@article{Atkins_Gershell_2002, title={From the analysts couch: Selective anticancer drugs}, volume={1}, url={http://dx.doi.org/10.1038/nrd842}, DOI={10.1038/nrd842}, number={7}, journal={Nature Reviews Drug Discovery}, publisher={Nature Publishing Group}, author={Atkins, Joshua H. and Gershell, Leland J.}, year={2002}, month={Jul}, pages={491-492}}=> #<StringIO:0x95cf914 @base_uri=#<URI::HTTP:0x95cfcd4 URL:http://data.crossref.org/10.1038%2Fnrd842>, @meta={"date"=>"Mon, 29 Jul 2013 22:54:09 GMT", "server"=>"Apache/2.2.3 (CentOS)", "x-powered-by"=>"Phusion Passenger (mod_rails/mod_rack) 3.0.7", "vary"=>"Accept", "access-control-allow-origin"=>"*", "x-content-type-options"=>"nosniff", "content-length"=>"351", "status"=>"200", "connection"=>"close", "content-type"=>"text/bibliography;charset=utf-8"}, @status=["200", "OK"]> 

創建一個文件來保存你的代碼,或許rtest.rb,這裏面的文件拷貝到下面的代碼:

require 'open-uri' 
open("http://dx.doi.org/10.1038/nrd842","Accept" =>"text/bibliography; style=bibtex"){|f| f.each {|line| print line}} 

保存文件,然後運行:

ruby rtest.rb 
+0

多謝你,就像一個夢! >歡迎來到紅寶石< –

+0

歡迎您,如果這完全回答你的原始問題,一定要選擇這是一個正確的答案!乾杯。 –