1

基于,我尝试使用 ruby​​ 将 DOI(文档对象标识符)发送到应返回书目信息的 crossref.org,在运行 Ruby 1.9.3-p194 的 WinXP 中从命令行进行以下调用:

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​​ 来执行其他任务,例如删除文件(喘气)。我想知道错误的原因可能是什么。

4

1 回答 1

1

看来您必须首先需要一个库,例如“open-uri”,如下例所示:

david@archbox:~$ 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
于 2013-07-29T22:56:22.700 回答