jjzjj

ruby-on-rails - `allow_any_instance_of` 模拟不在范围内工作

我的mock只有在如下所示的beforeblock中时才有效。这只是我对我的问题的快速而肮脏的表述。从字面上看,当我将行从beforeblock移动到doesnotquack断言时,它停止模拟:(describe'Ducks',type::featuredobeforedo...allow_any_instance_of(Duck).toreceive(:quack).and_return('bark!')visitanimal_farm_pathendcontext'isanoddduck'it'doesnotquack'doexpect(Duck.new.quack).toeq('

ruby-on-rails - Twilio 的未定义方法 `account'

我正在使用twilio并得到:错误undefinedmethod`account'forTwilio。client=Twilio::REST::Client.new('twilio_sid','twilio_token')#CreateandsendanSMSmessageclient.account.sms.messages.create(from:"+12345678901",to:user.contact,body:"Thanksforsigningup.Toverifyyouraccount,pleasereplyHELLOtothismessage.")

ruby-on-rails - 为什么 ruby​​-debug 说 'Saved frames may be incomplete'

我有时会在触发断点时收到此消息。看起来堆栈帧没有得到保存,所以我无法通过调用堆栈返回-真的很痛苦。看下面的例子-->#0BatchProcess.add_failure_record(row_id#Fixnum,test#Struct::Test,message#String,...)atlineserver/processes/batch.rb:309Warning:savedframesmaybeincomplete;comparewithcaller(0).(rdb:1)ppcaller["./server/processes/batch.rb:309:in`run_tests'

c - 我如何在 Ruby 的 C 扩展 API 上检索 'standalone' 符号

我想从C函数返回多个值,恕我直言,散列是一个不错的选择。我首先使用rb_intern('A_KEY')创建key,但扩展崩溃了。现在,我正在使用rb_str_new2,但我更喜欢符号。如何创建一个新符号,并在不引用类或方法的情况下使用它? 最佳答案 您需要使用ID2SYM宏将从rb_intern获得的标识符转换为ruby​​符号。尝试改变rb_intern('A_KEY')到ID2SYM(rb_intern('A_KEY')) 关于c-我如何在Ruby的C扩展API上检索'standal

ruby - Sinatra 应用程序中的错误 "undefined local variable or method ` logger '"when using ` logger.info`

我有以下Sinatra1.2.1应用程序代码:#app.rbrequire'sinatra'get'/'dologger.info"COUCOU"'Helloworld!'end并使用ruby-rubygemsapp.rb启动服务器。当我转到http://localhost:4567时出现错误:NameErrorat/undefinedlocalvariableormethod`logger'for#file:app.rblocation:blockinline:4我是否需要添加或配置一些东西才能在Sinatra中启用日志记录?阅读SinatraREADME和文档,似乎默认情况下为Si

ruby-on-rails - 为什么我在 Rails 中得到 "Errno::ECONNREFUSED"和 'net/http'?

我正在尝试从URL解析XML文件。当我尝试这样的事情时:require'net/http'require'rubygems'require'xmlsimple'url='http://my-address.com/xmltest/note.xml'xml_data=Net::HTTP.get_response(URI.parse(url)).body一切正常,但仅当我在Rails项目之外执行此操作时。如果我尝试将此文件包含在我的Rails3项目中并在那里解析它,我会收到错误消息"Errno::ECONNREFUSEDin[controller]"-Connectionrefused-c

ruby-on-rails - Ruby 挑战 - 方法链和惰性求值

看完文章http://jeffkreeftmeijer.com/2011/method-chaining-and-lazy-evaluation-in-ruby/,我开始寻找更好的方法链和惰性求值解决方案。我想我已经用以下五个规范概括了核心问题;谁能让他们全部通过?任何事情都可以:子类化、委托(delegate)、元编程,但不鼓励后者。最好将依赖性保持在最低限度:require'rspec'classFoo#EpiccodehereenddescribeFoodoit'shouldreturnanarraycorrespondingtothereverseofthemethodchai

ruby - Ruby 中的 OptionParse 和不以 '-' 开头的参数

我想要这样的参数:programdothis--additional--options和:programdothat--with_this_option=value我不知道该怎么做。我唯一能做的就是在开头使用带有--的参数。有什么想法吗? 最佳答案 要使用OptionParser处理位置参数,首先使用OptionParser解析开关,然后从ARGV中获取剩余的位置参数:#optparse-positional-arguments.rbrequire'optparse'options={}OptionParser.newdo|opts

ruby - 无法从 Jekyll 插件获取页面数据

我正在尝试为Jekyll编写一个自定义标签插件,它将输出站点上所有页面(不是帖子)的分层导航树。我基本上想要一堆嵌套的链接(以页面标题作为链接文本)指向具有特定CSS类标记的当前页面的页面。我对ruby​​非常缺乏经验。我是PHP专家。我想我应该从尝试遍历所有页面并输出一个一维列表开始,只是为了确保我至少可以做到这一点。这是我到目前为止所拥有的:moduleJekyllclassNavTree'site.pages.eachdo|page|output+=''+page.title+''endoutput+=''outputendendendLiquid::Template.regis

ruby - 'case' 语句如何使用常量?

我正在使用Ruby1.9.2和RubyonRails3.2.2。我有以下方法:#Note:The'class_name'parameterisaconstant;thatis,itisamodelclassname.defmy_method(class_name)caseclass_namewhenArticlethenmake_a_thingwhenCommentthenmake_another_thingwhen...then...elseraise("Wrong#{class_name}!")endend我想明白为什么在上面的case语句中,当我执行方法调用时它总是运行else“