草庐IT

ruby - 允许主机名包含下划线的 URI.parse 的替代方法

我正在使用DMOZ的listofurltopics,其中包含一些具有包含下划线的主机名的url。例如:608609TheOuterHeaven610InformationandimagegalleryofMcFarlane'sactionfiguresforTrigun,Akira,TenchiMuyoandotherJapaneseSci-Fianimations.611Top/Arts/Animation/Anime/Collectibles/Models_and_Figures/Action_Figures612虽然此url可以在网络浏览器中使用(或者至少在我的浏览器中可以使用:

ruby - 为什么 openssl 在 windows 上产生错误但在 centos 上不产生错误:PKCS12_parse: mac verify failure (OpenSSL::PKCS12::PKCS12Error)

require'openssl'ifARGV.length==2pkcs12=OpenSSL::PKCS12.new(File.read(ARGV[0]),ARGV[1])ppkcs12.certificateelseputs"Usage:load_cert.rb"end运行它会在Windows上产生错误,但在Linux上不会。错误:OpenSSL::PKCS12::PKCS12Error:PKCS12_parse:macverifyfailurefrom(irb):21:ininitializefrom(irb):21:innewfrom(irb):21fromC:/Ruby192/

ruby - create_additions 选项在 JSON.parse 中有什么作用

我在ruby​​sJSON.parse文档中遇到了create_additions选项,但我似乎找不到任何关于它的文档。rdoc描述了选项:create_additions:Ifsettofalse,theParserdoesn'tcreateadditionsevenifamatchingclassand::create_idwasfound.Thisoptiondefaultstotrue.出于好奇,谁能解释一下这个选项的实际作用以及JSON.parse的这个“附加”功能到底是什么? 最佳答案 “添加”是对纯JSON的添加。如果

ruby - 全新应用程序服务器上的 Rails 'parse_query' 错误

我已经在OSX10.10.3上安装了:自制软件,命令行工具,然后使用rbenv安装ruby​​:ruby2.2.2p95(2015-04-13revision50295)[x86_64-darwin14]Rails4.2.2当我创建一个新的应用程序(使用默认的sqlite数据库)并尝试运行它时,它说:=>BootingWEBrick=>Rails4.2.2applicationstartingindevelopmentonhttp://localhost:3000=>Run`railsserver-h`formorestartupoptions=>Ctrl-Ctoshutdownser

ruby-on-rails - 在 Ruby on Rails 中获取私有(private)方法错误

我有一个Controller:classStatsController它接受开始和结束时间并计算时间之间的持续时间。当我运行它时,出现以下错误:privatemethod`gsub!'calledforThuFeb0416:12:09UTC2010:Time为什么会这样? 最佳答案 私有(private)方法gsub!在使用Time.parse时调用通常意味着你已经调用了parse用Time对象而不是String所以听起来您的代码实际上是在尝试解析时间两次。例如>>t=Time.now=>FriFeb0513:12:17+00002

Ruby JSON.parse 返回一个数组

我正在尝试通过RESTfulAPI解析从Twitter收到的JSON字符串作为响应。我想使用JSONgem,但遇到了问题。根据文档,JSON解析器似乎返回了一个Hash,这正是我想要的。然而,当我实际执行JSON.parse(twitter_result.body)时,它返回一个数组。如何让JSON解析器返回哈希?这是我正在使用的解析器:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-parse“漂亮”的JSON输出示例:[{"created_at":"SunDec0813:00:52+0

ruby - nil.to_json 无法解析回 nil?

此代码段抛出异常:x=niljsoned=x.to_jsonputs'x.to_json='+jsoned.inspectputs'back='+JSON.parse(jsoned).inspectC:/ruby/lib/ruby/1.9.1/json/common.rb:146:in`parse':706:unexpectedtokenat'null'(JSON::ParserError)x.to_json="null"fromC:/ruby/lib/ruby/1.9.1/json/common.rb:146:in`parse'fromC:/dev/prototyping/appox

ruby-on-rails - 获取 URL 字符串参数?

我的数据库中有这个URL,在“位置”字段中:http://www.youtube.com/watch?v=xxxxxxxxxxxxxxxxxxx我可以通过@object.location获取,但是如何获取v的值呢?我的意思是,从URL字符串中获取"xxxxxxxxxxxx"? 最佳答案 require'uri'require'cgi'#useURI.parsetoparsetheURLintoitsconstituentparts-host,port,querystring..uri=URI.parse(@object.locati

ruby - 与 Ruby 的 CGI.parse 方法完全相反?

我想对查询参数进行一些清理。我用CGI.parse解析查询,然后我删除了一些参数,但我找不到相反的方法来构建查询。我真的不想做类似的事情params.map{|n,v|"#{CGI.escapen}=#{CGI.escapev.to_s}"}.join("&")必须有更简单的方法。有吗? 最佳答案 URI模块中有一个很好的方法:require'uri'URI.encode_www_form("q"=>"ruby","lang"=>"en")#=>"q=ruby&lang=en" 关于ru

ruby - 你如何让 DateTime.parse 返回你所在时区的时间?

我需要这个require'date'DateTime.parse"Mon,Dec276:30pm"返回EDT时区下午6:30的DateTime,但返回UTC时间。如何获取EST日期时间或将UTC日期时间转换为具有下午6:30值的EDT日期时间? 最佳答案 好的,我要回答我自己的问题require'time'ENV["TZ"]="US/Eastern"Time.parse("Mon,Dec276:30pm").to_datetime=># 关于ruby-你如何让DateTime.parse