草庐IT

from_uri

全部标签

Ruby 动态类。如何修复 "warning: class variable access from toplevel"

我正在尝试编写一个程序,根据从文件中读取的配置动态定义ruby​​类。我知道我可以使用Class.new来做到这一点。这是一个示例程序:x=[1,2,3]Test=Class.newdo@@mylist=xdeffooputs@@mylistendendTest.new.foo当我运行它时,我得到以下输出(使用ruby​​1.9.3p0运行):c:/utils/test.rb:4:warning:classvariableaccessfromtoplevelc:/utils/test.rb:7:warning:classvariableaccessfromtoplevel123Does

ruby - Ruby 中的 URI.escape 和 URI.encode 有什么区别?

我试图找出URI.escape和URI.encode之间的区别ruby。两者都没有按照我的意愿进行,即对URL进行完全编码。例如,我希望http://my.web.com为http%3A%2F%2Fmy%2Eweb%2Ecom 最佳答案 没有区别。在Ruby1.9.3中encodeissimplyanaliasforescape.[编辑]请注意,这些方法允许对字符的“不安全”描述符进行编码:URI.encode('http://my.web.com',/\W/)#=>"http%3A%2F%2Fmy%2Eweb%2Ecom"谢谢@m

Ruby: URI::InvalidURIError(URI 只能是 ascii

require'uri'uri=URI.parse'http://dxczjjuegupb.cloudfront.net/wp-content/uploads/2017/10/Оуэн-Мэтьюс.jpg'浏览器没有问题http://dxczjjuegupb.cloudfront.net/wp-content/uploads/2017/10/Оуэн-Мэтьюс.jpg所以我问自己这个ruby类是否有点过时了?我应该完全放弃它还是做一些错误处理…… 最佳答案 我通过问自己这个问题得到了答案:beginuri=URI.parse(

ruby - 如何在 Ruby 中将方案设置为 URI 对象

我正在尝试从用户输入中解析URI。我假设有些用户不会将方案放在他们的URI中,我想默认为“http”。以下代码无效:require'uri'uri_to_check=URI::parse("www.google.com")uri_to_check.scheme="http"unlessuri_to_check.schemeputsuri_to_check.to_s我希望看到“http://www.google.com”,但我得到的是“http:www.google.com”。甚至可以这样做吗?如果是这样,我错过了什么?有更好的方法吗? 最佳答案

ruby - 如何使用 httparty 切换 base_uri

我正在尝试将参数传递给login方法,我想根据该参数切换基本uri。像这样:classManagementdbincludeHTTPartydefself.login(game_name)casegame_namewhen"game1"self.base_uri="http://game1"when"game2"self.base_uri="http://game2"when"game3"self.base_uri="http://game3"endresponse=self.get("/login")ifresponse.success?@authToken=response["au

Ruby:检查 URI 是否为 HTTPS?

我想检查URI是否需要SSL身份验证:url=URI.parse("http://www.google.com")#[somecode]ifurl.instance_of?URI::HTTPShttp.use_ssl=truehttp.verify_mode=OpenSSL::SSL::VERIFY_NONEend但是,那几行抛出以下错误../usr/lib/ruby/1.8/uri/common.rb:436:in`split':badURI(isnotURI?):HTTPS(URI::InvalidURIError)from/usr/lib/ruby/1.8/uri/common.

ruby-on-rails - rails : Copying attributes from an object to another using the "attributes" method

让模型Quote具有属性[price,description]让模型Invoice有属性[price,description,priority]让invoice模型Invoice中的对象具有属性{price:10,description:'lamp',priority:10}invoice={price:10,description:'lamp',priority:10}假设我想将invoice属性复制到新的quote。quote=Quote.new(invoice.attributes)这会引发一个错误,即priority在模型Quote中不存在。如何将invoice属性复制到新的q

ruby - 加载网页时 : "undefined method ` request_uri' for #"

我正在尝试使用Ruby通过HTTP加载网页并检查其状态代码是什么。我的代码如下所示:require"net/http"@r=Net::HTTP.get_response(URI.parse(myURL))return@r.code但是,对于某些URL(主要是指向奇怪的东西,例如不会给出正确响应的Web计数器),我得到了一个undefinedmethodrequest_urifor#异常。我已经将它追溯到http.rb的第380行(我正在运行Ruby1.8),它说:defHTTP.get_response(uri_or_host,path=nil,port=nil,&block)ifpa

ruby-on-rails - Rails 时间奇数 : "x days from now"

当用户注册到我的网站之一进行免费试用时,我将他们的帐户到期时间设置为“14.days.from_now”。然后在主页上我显示他们还剩多少天,这是我得到的:(user.trial_expires-Time.now)/86400(因为一天有86400秒,即60*60*24)有趣的是,结果超过14,因此四舍五入为15。在控制台中进行更仔细的调查后,这种情况只会在未来两天发生(如果您明白我的意思)。例如>>Time.now=>FriOct2911:09:2601002010>>future_1_day=1.day.from_now=>Sat,30Oct201011:09:27BST01:00#

ruby-on-rails - rails : remove decimal from number_to_currency

我有一个float价格:number_to_currency(m.price,:locale=>'en_us')我得到:$39.00如何删除.00,我想得到:$39 最佳答案 您可以按照记录将精度设置为0here在RailsAPI文档中。number_to_currency(m.price,locale::en,precision:0)请注意,您的价格将进行四舍五入,从38.50美元到39.49美元的任何价格都将显示为39美元。编辑:将区域设置:en_us替换为:en,这可能会在更多应用中启用。