草庐IT

http远程调用

全部标签

ruby - Ruby 中 &(& 符号)的用途用于过程和调用方法

我注意到很多处理RubyProcs的示例中都有以下&符号。#RubyExampleshout=Proc.new{puts'Yolo!'}defshout_n_times(n,&callback)n.timesdocallback.callendendshout_n_times(3,&shout)#prints'Yolo!'3times我的问题是&符号背后的功能目的是什么?看起来,如果我在不使用&的情况下编写完全相同的代码,它会按预期工作:#Samecodeaspreviouswithout&shout=Proc.new{puts'Yolo!'}defshout_n_times(n,ca

ruby-on-rails - 如果我调用 Factory.build 以使我的 Controller 测试快速,我怎样才能让 Factory Girl 永远不会访问数据库?

我正在寻求使我的Rails测试更快。我只有520个测试,但它们在bash中运行需要62秒,在Rubymine中运行需要82秒。作为典型Controller测试的示例,我使用此代码以@user身份登录并在CommentsController中为我的RSpecController测试创建基本的@comment:before(:each)do@user=Factory.create(:user)sign_in@user@comment=Factory.create(:comment)end您可能会意识到...这很慢。它构建了一个@user,但也为该用户构建了关联。@comment也是如此。所

ruby - 为什么 Ruby 方法调用特别慢(与其他语言相比)?

我正在尝试阅读有关Ruby性能的信息,并遇到了thisSOthread,其中一个答案提到“方法调用,Ruby中最常见的操作之一,特别慢。”Anotherthread提到“它对方法进行“后期查找”,以提供灵active。这会减慢它的速度。它还必须记住每个上下文的名称以允许eval,因此它的框架和方法调用速度较慢。”有人可以更详细地解释为什么Ruby方法调用特别慢,并详细说明第二个线程吗?我不完全确定延迟查找是什么或为什么它很慢,而且我不知道每个上下文的名称是什么意思或它与框架和方法调用的关系。我(可能不正确)的理解是,由于可以在运行时添加或修改方法,Ruby解释器永远不会“记住”如何运行

ruby - 如何从 gem 远程源中删除远程源

我已将gemcutter.org添加到我的Rubygems源中,现在我不知道如何删除它。$gemsources***CURRENTSOURCES***http://gemcutter.orghttp://gems.rubyforge.org/ 最佳答案 $gemsources-rhttp://gemcutter.orghttp://gemcutter.orgremovedfromsources$gemsources***CURRENTSOURCES***http://gems.rubyforge.org/

ruby - 调用 View 文件时如何传递参数?

我使用Sinatra和Haml编写了一个网络表单,将用于调用Ruby脚本。一切似乎都很好,除了一件事:我需要从Sinatra/Ruby脚本向HamlView文件传递一个参数。这是我的部分代码:#!/usr/bin/envrubyrequire'rubygems'require'sinatra'require'haml'get'/'dohaml:indexendpost'/'doname=params[:name]vlan=params[:vlan]tmp=niltmp=%x[./wco-hosts.rb-a-n#{name}-v#{vlan}]iftmp.include?("Error

ruby-on-rails - 获取远程图像尺寸的快速方法

我正在使用imagesizegem检查远程图像的大小,然后只将足够大的图像推送到数组中。require'open-uri'require'image_size'data=Nokogiri::HTML(open(url))images=[]forcenocache=Time.now.to_i#Nocachebecausejqueryloadeventdoesn'tfireforcachedimagesdata.css("img").eachdo|image|image_path=URI.join(site,URI.encode(image[:src]))open(image_path,"

ruby-on-rails - 预期定义。在模块内调用类时

我是Rails的新手。我在lib目录中有一个这样的设置:lib/blog/core/search/base.rbbase.rb也定义了Base类:moduleBlogmoduleCoremoduleSearchclassBaseattr_accessor:propertiesdefinitialize(params)@properties={}endendendendend我的application.rb中有以下代码config.autoload_paths+=Dir["#{config.root}/lib/**/"]当我将它包含在postsController中时,出现以下错误:Lo

ruby - 如何从包含模块的类调用 Ruby 模块中的静态方法?

是否可以在ruby​​模块中声明静态方法?moduleSoftwaredefself.exitputs"exited"endendclassWindowsincludeSoftwaredefself.startputs"started"self.exitendendWindows.start上面的例子不会打印出“exited”。模块中只能有实例方法吗? 最佳答案 像这样定义您的模块(即使exit成为模块中的实例方法):moduleSoftwaredefexitputs"exited"endend然后使用extend而不是includ

ruby - 从 Ruby HTTP 请求中获取响应 header

我正在使用Net::HTTP通过Ruby发出HTTP请求,但我不知道如何获取所有响应header。我尝试了response.header和response.headers但没有任何效果。 最佳答案 响应对象实际上包含标题。有关详细信息,请参阅“Net::HTTPResponse”。你可以这样做:response['Cache-Control']您还可以在响应对象上调用each_header或each以遍历header。如果您真的想要响应对象之外的header,请调用response.to_hash

ruby-on-rails - 在 env.rb 之外需要 Cucumber-rails。其余的加载被推迟到 env.rb 被调用

请问这个env.rb错误是什么意思?root#rakedb:migrateWARNING:Cucumber-railsrequiredoutsideofenv.rb.Therestofloadingisbeingdefereduntilenv.rbiscalled.Toavoidthiswarning,move'gemcucumber-rails'underonlygroup:testinyourGemfilegemfile在这里:source'http://rubygems.org'gem'rails','3.1.0'#BundleedgeRailsinstead:#gem'rail