草庐IT

instance-groups

全部标签

ruby-on-rails - RSpec::Mocks::ExampleMethods 的未定义方法 instance_double

我有一个这样的测试用例:describeWorkCardsControllerdoit"something"dowork_card=instance_double(WorkCard,{:started?=>true})#somemorecodeendend当我运行RSpec时,出现错误:undefinedmethod'instance_double'for#根据http://rubydoc.info/github/rspec/rspec-mocks/RSpec/Mocks/ExampleMethods这种方法存在。所以我尝试通过以下方式直接访问它:describeWorkCardsCo

ruby - 可能对 curry 过程进行 instance_eval 吗?

假设我有这样一个类:classTestdeftest_func140endend还有一个proc,它引用了Test中的一个成员函数:p=->(x,y){x+y+test_func}#=>#要调用p,我将它绑定(bind)到Test的一个实例:test=Test.new#=>#test.instance_exec(1,2,&p)#=>143现在假设我只想将y传递给p,并且始终传递x=1:curried=p.curry[1]#=>#理想情况下,我应该能够像以前一样instance_exec,但是:test.instance_exec(2,&curried)=>NameError:undef

ruby - 我可以将一个本身需要一个 block 的 block 传递给 ruby​​ 中的 instance_exec 吗?

我期待代码foo=proc{puts"foo"}instance_exec(1,2,3,&foo)do|*args,&block|puts*argsblock.callputs"bar"end输出123foobar但是报错bothblockargandactualblockgiven我可以将一个本身需要一个block的block传递给ruby​​中的instance_exec吗? 最佳答案 &foo尝试将foo作为block传递给instance_exec,而您已经传递了一个显式block。省略与号发送foo就像任何其他参数一样(除

ruby - 模块嵌套在 instance_eval/exec 或 module_eval/exec 中

我在尝试回答this时想到了这个问题.以下是预期的行为:moduleApModule.nestingend#=>[A]但是以下内容:A.instance_eval{pModule.nesting}A.instance_exec{pModule.nesting}A.module_eval{pModule.nesting}A.module_exec{pModule.nesting}全部返回[]。为什么这些不能像上面那样工作?附加问题Muistooshort提出了一个有趣的观点。如果这是正确的,那么Module.nesting将是依赖于文字上下文的方法和变量之一,例如Method#sourc

ruby - 如何像 instance_eval 方法那样在一个 block 中改变 self 呢?

instance_eval方法在其block中改变自身,例如:classD;endd=D.newd.instance_evaldoputsself#printsomethinglike#,not'main'!end如果我们自己定义一个方法(或任何其他方法(除了instance_eval)需要一个block),当打印self时,我们将得到'main',这与instance_eval方法不同。例如:[1].eachdo|e|putsself#print'main'end我如何定义一个像instance_eval这样的方法(需要一个block)?提前致谢。 最佳答

ruby-on-rails - Rails 3.1 与 PostgreSQL : GROUP BY must be used in an aggregate function

我正在尝试加载按user_id分组并按created_at排序的最新10个艺术。这适用于SqlLite和MySQL,但在我的新PostgreSQL数据库上出错。Art.all(:order=>"created_atdesc",:limit=>10,:group=>"user_id")ActiveRecord错误:ArtLoad(18.4ms)SELECT"arts".*FROM"arts"GROUPBYuser_idORDERBYcreated_atdescLIMIT10ActiveRecord::StatementInvalid:PGError:ERROR:column"arts.i

ruby - 传递给 `instance_exec` 时如何执行 proc

问题的灵感来自thisone.Proc::new有一个选项可以在方法内部没有block的情况下调用:Proc::newmaybecalledwithoutablockonlywithinamethodwithanattachedblock,inwhichcasethatblockisconvertedtotheProcobject.当proc/lambda实例作为代码块传递时,将创建Proc的新实例:Proc.singleton_class.prepend(Module.newdodefnew(*args,&cb)puts"PROC#{[block_given?,cb,*args].i

ruby-on-rails - rails - 使用 Group_by 时 - 如何获取索引?

我有以下内容:sets=DataSet.all.group_by{|data|[data.project_id,"-",data.thread_id].join("")}:LastPost问题是我需要一个索引。所以我更新了上面的内容::LastPost然后中断,出现错误:undefinedmethod`last'for0:Fixnum想法?谢谢 最佳答案 您观察到的问题是因为参数分配给block的方式。在您的第二个示例中,您将观察到range包含一个包含单个range和匹配的datas的数组,datas变量包含索引,i始终为nil。

ruby : how to prevent modification of an array instance variable through an attribute reader

抱歉这个菜鸟问题...假设我们有:classTestMeattr_reader:arraydefinitialize@array=(1..10).to_aend结束然后可以这样做:>>a=TestMe.new=>#>>a.array.map!&:to_s=>["1","2","3","4","5","6","7","8","9","10"]>>a.array=>["1","2","3","4","5","6","7","8","9","10"]这显然不利于封装,不是吗?有什么方法可以快速保护数组变量不被更改吗?...或者每当我的实例变量具有“破坏性”方法时,我是否需要实现一个深拷贝读取

ruby - 为什么 instance_eval 对 Proc 成功但对 Lambda 不成功?

我有以下类(class):classUsercode1=Proc.new{}code2=lambda{}define_method:testdoself.class.instance_eval&code1self.class.instance_eval&code2endendUser.new.test为什么第二个instance_eval失败并出现错误数量的参数(1代表0)错误? 最佳答案 instance_eval正在将self(User)生成给lambda。Lambda对其参数有特殊要求-方法也是如此-如果参数太少/太多,将引发