草庐IT

default_proc

全部标签

ruby - 我如何在 Ruby 中动态调用 Proc?

proc有method.send等价物吗?例如:defsaa+1endb="s"sendb.to_sym,10#=>11有这样的东西吗?p=Proc.new{|s|s+1}d="p"*calld.to_sym,10*编辑:针对mudasobwa的回答我需要从一系列方法中调用Procs。例如:ss=["a","b","c","d"]在这种情况下可能吗? 最佳答案 其他答案涵盖了所问的确切问题。但我要说,这是一种错误的做法。不要进行运行时自省(introspection)。它没有带来任何好处。如果您想通过名称来处理您的过程,请将它们放在

ruby-on-rails - 参数错误 : assertion message must be String or Proc using assert_select

我正在使用testunit2.4.0为Rails3.1应用编写Controller测试。我想断言某个标题没有出现在页面上。我像这样使用assert_select:assert_select'h1',{:text=>/KeyDates/,:count=>0}出现以下错误:ArgumentError:assertionmessagemustbeStringorProc:expectedbutwas.>()我已经追踪到assert_select调用build_message的事实,它创建了AssertionMessage的实例并将其传递给测试-单位的断言。然而,在testunit的2.2版(

ruby - 为什么 RVM 卡在 "#importing default gemsets, this may take time"上?

当我使用RVM安装Ruby时,这会显示在我的终端session中:ks@ks-mba~$rvmreinstall1.9.3--with-readline-dir=$rvm_path/usrRemoving/Users/ks/.rvm/src/ruby-1.9.3-p429...-usingZSH,cannotshowprogress,bepatient...Removing/Users/ks/.rvm/rubies/ruby-1.9.3-p429...-usingZSH,cannotshowprogress,bepatient...Installingrequirementsforos

ruby - 如何在 `&` -`&` 往返下保存 proc 对象?

当我在将数组传递给方法时用*展开一个数组,然后在方法中用*从它重建一个数组,数组的标识不是保留:a=[]a.object_id#=>69846339548760defbar*a;a.object_idendbar(*a)#=>69846339537540但是,当我将proc传递给方法时使用&将其转换为block,然后在方法中使用&从block重建proc时,过程的身份似乎被保留了下来:pr=->{}pr.object_id#=>69846339666160deffoo≺pr.object_idendfoo(&pr)#=>69846339666160proc对象是如何保存的?转成b

Ruby Symbol#to_proc 在 1.9.2-p180 中泄漏引用?

好的,这是我第二次尝试调试Sinatra应用程序的内存问题。我相信这次我已经将它确定为简单的示例代码。似乎当我通过.map(&:some_method)过滤数组时,它会导致该数组中的项目无法被垃圾回收。运行等效的.map{|x|x.some_method}完全没问题。演示:给定一个简单的示例类:classCdeffoo"foo"endend如果我在IRB中运行以下命令,它会被正常收集:ruby-1.9.2-p180:001>a=10.times.map{C.new}=>[...]ruby-1.9.2-p180:002>b=a.map{|x|x.foo}=>["foo","foo","f

ruby-on-rails - 如何取消 join/eager_load 中的 default_scope?

我有两个模型:classUserdefault_scope->{where(deleted_at:nil)}endclassOrderbelongs_to:userend我想获得已删除或未删除用户的订单:Order.joins(:user).merge(User.unscoped)Order.joins(:user).merge(User.unscope(where::deleted_at))#SELECT"orders".*FROM"orders"#INNERJOIN"users"ON"users"."id"="orders"."user_id"AND"users"."deleted

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 - 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 - 在 Chef 的提供者的 default.rb 中使用 File::read

我正在尝试创建一个LWRP,它将调用在其自身中定义的资源。我的Recipe结构如下:在机器Recipe的提供者中,我有如下代码片段:require'chef/provisioning'#driverforcreatingmachinesrequire'::File'defget_environment_json@@environment_template=JSON.parse(File::read(new_resource.template_path+"environment.json"))return@@environment_templateend代码只是试图读取一个json文件,