jjzjj

any_instance

全部标签

ruby - RSpec any_instance 返回 self

我正在尝试stub某个类的任何实例。我需要stubfetch方法,它用一些数据填充self。如何访问self变量,修改它并返回fetch方法?MyObject.any_instance.stub(:fetch){self}不返回MyObject实例。也许,模拟在这种情况下更有用。不幸的是,我还没有理解它们。 最佳答案 有一个openrspec-mocksissue解决这个问题。我希望在某个时候解决这个问题,但是以一种不破坏现有规范套件的方式添加它并不简单,这些规范套件使用any_instanceblock实现,因为我们会开始屈服一个

ruby - 总和(&:x) not working any more

我在我的Rails应用程序(4.1.2)中使用payments.sum(&:price)。自从我从Ruby1.9.3更新到2.1.2后,出现了以下错误:wrongnumberofarguments(1for2..3)这些变体有效:payments.map(&:price).sumpayments.to_a.sum(&:price)我必须重写我的代码还是我遗漏了什么?谢谢! 最佳答案 来自documentation:sum(*args)Calculatesthesumofvaluesonagivencolumn.Thevalueisr

ruby - 获取名称错误 : `format' is not allowed as an instance variable name when testing instance variable with rspec

我有以下测试:let(:client){Descat::Client.new}describe'poblacio'doit'shouldsetformatcorrectly'doclient.poblacio('v1','json','dades')expect(client.instance_variable_get(:format)).toeq('json')endend我有以下正在测试的代码:moduleDescatclassClientBASE_URL='http://api.idescat.cat/'definitialize(attributes={})attributes

Ruby 单例方法 (class_eval, define_method) vs (instance_eval, define_method)

Ruby中的元编程很棒,因为我经常使用它来模拟基于原型(prototype)的编程,并快速编写一些问题的原型(prototype)解决方案来测试它们的可行性。所以我想知道下面这段代码是否有本质区别:(class和(class代码的两个版本都定义了一个单例方法,我还没有遇到任何迫使我选择(instance_eval,define_method)组合而不是(class_eval,define_method)组合来定义单例方法,我想知道两者之间是否存在一些本质区别。 最佳答案 define_method没有区别。但是当您使用def时会有所

ruby - 'yield self' 和 instance_eval 一样吗?

如果用instance_eval:定义Foo有什么不同吗?..classFoodefinitialize(&block)instance_eval(&block)ifblock_given?endend。..或者使用“yieldself”:classFoodefinitializeyieldselfifblock_given?endend无论哪种情况,您都可以这样做:x=Foo.new{deffoo;'foo';end}x.foo所以“yieldself”意味着Foo.new之后的block总是在Foo类的上下文中求值。这是正确的吗? 最佳答案

ruby-on-rails - instance_eval 是如何工作的,为什么 DHH 讨厌它?

大约在hisRailsConfpresentation的19:00点,DavidHeinemeierHansson谈到了instance_eval的缺点:ForalongtimeIrantedandravedagainstinstance_eval,whichistheconceptofnotusingayieldedparameter(likedo|people|)andjuststraightdosomethingandthenevaluatewhat'sinthatblockwithinthescopeofwhereyoucamefrom(Idon'tevenknowifthat

ruby - 使用 class_eval 和 instance_eval 访问 Ruby 类变量

我有以下内容:classTest@@a=10defshow_a()puts"a:#{@@a}"endclass为什么以下工作:Test.instance_eval{show_b}b:40=>nil但是我不能直接访问@@b?Test.instance_eval{@@b}NameError:uninitializedclassvariable@@binObject同样,下面的工作t=Test.newt.instance_eval{show_a}a:10=>nil但以下失败t.instance_eval{@@a}NameError:uninitializedclassvariable@@ai

ruby - 有没有办法在 Test::Unit 中撤消 any_instance 的摩卡 stub

很像thisquestion,我也在使用RyanBates的nifty_scaffold。它具有使用Mocha的any_instance的理想方面。在Controller后面的模型对象中强制进入“无效”状态的方法。与我链接到的问题不同,我没有使用RSpec,而是使用Test::Unit。这意味着那里的两个以RSpec为中心的解决方案对我不起作用。是否有通用的(即:与Test::Unit一起使用)删除any_instancestub的方法?我认为它导致我的测试出现错误,我想验证这一点。 最佳答案 碰巧,Mocha0.10.0允许uns

Ruby 的 def 和 instance_eval 与 class_eval

我正在阅读ProgrammingRuby1.9的元编程部分,但我无法理解class_eval之间内部发生了什么|/class_exec与instance_eval/instance_exec.首先,我的理解是def在self的方法表中添加一个方法(类对象):classAputsself#=>Adeffoo;42;end#addedtothemethodtableofself,sobecomesaninstancemethodendA.new.foo#=>42如果我们使用class_eval,我们得到相同的行为:A.class_evaldoputsself#=>Adefbar;42;en

ruby - @instance_variable 和 attr_accessor 的区别

我刚开始学习ruby​​,我看不出@instace_variable和使用attr_accessor声明的属性之间的区别。下面两个类有什么区别:classMyClass@variable1end和classMyClassattr_accessor:variable1end我在网上查了很多教程,每个人使用的表示法都不一样,这和ruby版本有什么关系吗?我还在StackOverflow中搜索了一些旧线程Whatisattr_accessorinRuby?What'stheDifferenceBetweenTheseTwoRubyClassInitializationDefinitions?