草庐IT

exec_proc

全部标签

ruby - 为什么 to_proc 在 Ruby 改进中不起作用?

to_proc似乎不适用于细化中定义的方法:moduleArrayExtensionsrefineArraydodefsumreduce(0,:+)endendendusingArrayExtensionsputs[[1,2,3]].map{|array|array.sum}#=>6puts[[1,2,3]].map(&:sum)#=>array.rb:13:in`map':undefinedmethod`sum'for[1,2,3]:Array(NoMethodError)puts[1,2,3].method(:sum).to_proc.call#=>array.rb:14:in`m

ruby-on-rails - 在 Rails 中运行 bundle exec rspec spec/requests/static_pages_spec.rb 时出错

我正在关注这篇文章,我可以在下面的这个ruby​​文件中编写这段代码,主页确实有示例应用程序,但当我运行bundleexec时,它仍然说静态页面主页应该有内容“示例应用程序”rspec规范/requests/static_pages_spec.rbspec/requests/static_pages_spec文件代码:require'spec_helper'describe"Staticpages"dodescribe"Homepage"doit"shouldhavethecontent'SampleApp'"dovisit'/static_pages/home'page.should

ruby - Ruby 1.9 中更自然的 Proc 调用方式

我们知道,在Ruby1.9中有几种Proc调用方式f=->n{[:hello,n]}pf[:ruby]#=>[:hello,:ruby]pf.call(:ruby)#=>[:hello,:ruby]pf.(:ruby)#=>[:hello,:ruby]pf===:ruby#=>[:hello,:ruby]我很好奇,调用Proc的更“自然”方式是什么?“自然”可能意味着更像计算机科学。 最佳答案 第二个选项是迄今为止最常用的。pf.call(:ruby)#=>[:hello,:ruby]它使它更类似于标准方法。此外,一些库在验证参数检

ruby - Symbol#to_proc 自定义方法

我喜欢在Ruby中如何将方法作为block传递,就像这样使用Symbol#to_proc:[1.0,2.0,3.0].map(&:to_i)#=>[1,2,3]我还可以定义自己的lambda,times_two,并将其作为block传递:times_two=->(x){x*2}[1,2,3].map(×_two)#=>[2,4,6]虽然我似乎不能将times_two作为符号传递:[1,2,3].map(&:times_two)#=>ArgumentError:wrongnumberofarguments(0for1)然而,当我尝试用一​​种方法做同样的事情时,我得到了一个错误

ruby - 一元运算符和在 Ruby 中将 proc 作为参数传递

我无法理解下面这段代码。我想到了一元与号运算符并将proc作为参数传递给方法。但我真的无法将self传递给language.call。我是这样理解的:我们将self作为参数传递给proc/block语言。这对我来说没有任何意义。有人可以解释一下吗?:)classTranslatordefspeak&languagelanguage.call(self)endprotecteddeffrench'bonjour'enddefspanish'hola'enddefturkey'gobble'enddefmethod_missing(*args)'awkwardsilence'endend我

ruby - 您已经激活了 rack 1.3.2,但是您的 Gemfile 需要 rack 1.2.3。考虑使用 bundle exec

我在尝试运行我的应用程序时遇到了问题:Youhavealreadyactivatedrack1.3.2,butyourGemfilerequiresrack1.2.3.Considerusingbundleexec.我读了很多关于这种错误的资料,但我没有找到适合我的解决方案我已经按照建议删除了我的Gemfile.lock并重新运行包here我已经在使用最新版本的passenger(3.0.8)-按照建议herebundleexecrake技巧不能用于我的情况谢谢你提前 最佳答案 运行bundleinstall--binstubs,您

ruby-on-rails - 无法使用 bundle exec 找到 rake

当我尝试执行“bundleexecrakeanything”时,出现错误:Couldnotfindrake-10.1.0inanyofthesources.Run`bundleinstall`toinstallmissinggems.但是当我执行简单的'rakeanything'时,rake正在工作。当然,我以任何方式尝试设置gem,我知道:gem安装rake-v=10.1.0在Gemfile中写入“gem'rake','10.1.0'”,然后执行bundleinstall我为所有rvm安装了gem:rvmalldogeminstallrake-v10.1.0这种情况真的很令人沮丧,因

ruby - foo(&nil) 与 foo(&"not a proc") 的行为有何不同?

我从质问中发现[1,2,3].each(&nil)不会导致任何错误-它只是返回一个枚举器。相比之下,[1,2,3].each(&"")加注TypeError:wrongargumenttypeString(expectedProc)此外,&nil会导致block_given?返回假defblock_given_testerifblock_given?puts"Blockgiven"elseputs"Blocknotgiven"endendblock_given_tester(&nil)#=>Blocknotgiven这不是因为NilClass实现了to_proc-我检查了RDoc。我能

ruby-on-rails - 为什么 ruby​​ 中的 break 语句在使用 Proc.new 和符号时表现不同?

block的break语句(根据TheRubyProgrammingLanguage)定义如下:itcausestheblocktoreturntoitsiteratorandtheiteratortoreturntothemethodthatinvokedit.因此,当运行以下代码时,会导致LocalJumpError。deftestputs"enteringtestmethod"proc=Proc.new{puts"enteringproc";break}proc.call#LocalJumpError:iteratorhasalreadyreturnedputs"exitingt

ruby - `exec' : string contains null byte (ArgumentError)

cmd="snvco#{rep}--username#{svn_user}--password#{pxs}"putscmd#thiscodewotksandprintsallvarsvaluesnormallyexec(cmd)xpto.rb:69:in`exec':stringcontainsnullbyte(ArgumentError)fromxpto.rb:69$ruby-vruby1.8.7(2010-01-10patchlevel249)[i686-linux]$gem-v1.3.7这是怎么回事?我该如何解决这个问题? 最佳答案