jjzjj

open-instance_method

全部标签

ruby-on-rails - 运行 Heckle 时出错? `current_code' : undefined method `translate' for Ruby2Ruby

我正在尝试运行Heckle,但一直出现错误:>specspec/controllers/my_controller_spec.rb--heckleMyController!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!exception=hasathickskin.There'snothingtoheckle.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(Runsthroughseveralmu

ruby - 如何 to_enum( :method) receive its block here?

这段代码来self发现的一个示例,计算数组中等于其索引的元素数。但是怎么办?[4,1,2,0].to_enum(:count).each_with_index{|elem,index|elem==index}我无法仅通过链接来完成,而且链中的求值顺序令人困惑。我的理解是我们正在使用Enumerable#count的重载,如果给定一个block,它会计算产生真值的元素的数量。我看到each_with_index具有判断项目是否等于其索引的逻辑。我不明白的是each_with_index如何成为count的block参数,或者为什么each_with_index像被调用一样工作直接在[4,

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 习语 : method call or else default

在Ruby中执行此操作的正确方法是什么?defcallOrElse(obj,method,default)ifobj.respond_to?(method)obj.__send__(method)elsedefaultendend 最佳答案 因为还没有提供答案:result=obj.methodrescuedefault与@slhck一样,当我知道obj很有可能不会响应方法时,我可能不会使用它,但这是一个选项。 关于Ruby习语:methodcallorelsedefault,我们在St

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 - open-uri 从以 iso-8859 编码的网页返回 ASCII-8BIT

我正在使用open-uri读取一个声称以iso-8859-1编码的网页。当我读取页面内容时,open-uri返回一个以ASCII-8BIT编码的字符串。open("http://www.nigella.com/recipes/view/DEVILS-FOOD-CAKE-5310"){|f|pf.content_type,f.charset,f.read.encoding}=>["text/html","iso-8859-1",#]我猜这是因为网页中的字节(或字符)\x92不是有效的iso-8859字符。http://en.wikipedia.org/wiki/ISO/IEC_8859-

ruby - Ruby 1.8.7 中的 File.open 和 block

我是ruby​​的新手,目前正在阅读Pickaxe一书以熟悉所有内容。我遇到了File.open部分,其中讨论了将block作为File.open调用的参数,然后保证文件已关闭。现在,这听起来绝对是避免搬起石头砸自己脚的绝妙方法,而且由于我的脚趾很危险,所以我想我会试一试。这是我写的(如果重要的话,在irb中):File.open('somefile.txt','r').each{|line|putsline}``我的预期是文件somefile.txt会被打开、读取、打印和关闭,对吗?据我所知是错误的。如果我使用lsof查看打开的文件句柄,它仍然是打开的。但是,如果我这样做f=File

ruby-on-rails - Railscasts 第 362 集 - 导出到 Excel : How to avoid the warning message given by Excel 2010 when opening the file?

当使用RyanBates的Railscasts第362集关于导出到Excel(https://github.com/railscasts/362-exporting-csv-and-excel)的示例应用程序时,我注意到Excel2010(在Windows上)在打开.xls文件时给我一条警告消息我使用“下载为Excel”链接下载的文件。警告内容如下:“您尝试打开的文件...的格式与文件扩展名指定的格式不同。打开文件前请确认文件未损坏且来源可靠。是否要打开现在存档吗?”当我单击"is"时,我可以很好地打开文件。在使用Excel2011(在Mac上)时,我什至没有收到警告消息。但我希望能够

ruby-on-rails - 覆盖类和实例方法的 method_missing?

我正在尝试编写一个通用模块,以将用于动态方法创建的method_missing模式应用于我的某些Rails模型。这些模型既有类方法也有实例方法。虽然我可以相当直接地为任一类案例编写模块:moduleClassVersionextendActiveSupport::ConcernmoduleClassMethodsdefmethod_missing(meth,*args,&block)ifmeth.to_s=~/^(.+)_async$/Async::handle_asyncself,$1,*args,&blockelsesupermeth,*args,&blockendend#Logi

ruby-on-rails - 无法安装 gems,因为 "undefined method ` invoke_with_build_args' for nil :NilClass"

我在Rubyrails上,我正在安装RubyonRails。我正在尝试安装gems,但它没有发生,我不确定为什么以及如何修复它。$geminstallbundlerERROR:Loadingcommand:install(LoadError)dlopen(/Users/nthulanemakgato/.rbenv/versions/2.1.1/lib/ruby/2.1.0/x86_64-darwin13.0/openssl.bundle,9):Librarynotloaded:/usr/local/opt/openssl/lib/libssl.1.0.0.dylibReferenced