草庐IT

label_from_instance

全部标签

ruby - RSpec any_instance 返回 self

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

ruby-on-rails - rails : Copying attributes from an object to another using the "attributes" method

让模型Quote具有属性[price,description]让模型Invoice有属性[price,description,priority]让invoice模型Invoice中的对象具有属性{price:10,description:'lamp',priority:10}invoice={price:10,description:'lamp',priority:10}假设我想将invoice属性复制到新的quote。quote=Quote.new(invoice.attributes)这会引发一个错误,即priority在模型Quote中不存在。如何将invoice属性复制到新的q

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-on-rails - Rails 时间奇数 : "x days from now"

当用户注册到我的网站之一进行免费试用时,我将他们的帐户到期时间设置为“14.days.from_now”。然后在主页上我显示他们还剩多少天,这是我得到的:(user.trial_expires-Time.now)/86400(因为一天有86400秒,即60*60*24)有趣的是,结果超过14,因此四舍五入为15。在控制台中进行更仔细的调查后,这种情况只会在未来两天发生(如果您明白我的意思)。例如>>Time.now=>FriOct2911:09:2601002010>>future_1_day=1.day.from_now=>Sat,30Oct201011:09:27BST01:00#

ruby-on-rails - rails : remove decimal from number_to_currency

我有一个float价格:number_to_currency(m.price,:locale=>'en_us')我得到:$39.00如何删除.00,我想得到:$39 最佳答案 您可以按照记录将精度设置为0here在RailsAPI文档中。number_to_currency(m.price,locale::en,precision:0)请注意,您的价格将进行四舍五入,从38.50美元到39.49美元的任何价格都将显示为39美元。编辑:将区域设置:en_us替换为:en,这可能会在更多应用中启用。

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 - rails : Render view from outside controller

我正在尝试使用View创建HTML字符串。我想从一个不是Controller的类中呈现它。如何在Controller外使用Rails渲染引擎?类似于ActionMailer的做法?谢谢! 最佳答案 Rails5和6以更方便的方式支持这一点,在幕后处理创建请求和诸如此类的事情:rendered_string=ApplicationController.render(template:'users/show',assigns:{user:@user})这会呈现app/views/users/show.html.erb并设置@user实例

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