草庐IT

el4r-instance

全部标签

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 - 更新到 OS X El Capitan (10.11) 后 compass 不工作

这个问题在这里已经有了答案:Can'tinstallgemsonOSX"ElCapitan"(15个答案)关闭7年前。安装OSXElCapitan(10.11)后,我的ruby​​compass不再工作。尝试安装compass后,我收到如下错误消息:$sudogeminstallcompassERROR:Whileexecutinggem...(Errno::EPERM)Operationnotpermitted-/usr/bin/compass如何解决这个问题?

vue2+element-ui,el-aside侧边栏容器收缩与展开

一、概览实现效果如下:二、项目环境1、nodejs版本node-vv16.16.02、npm版本npm-vnpmWARNconfigglobal`--global`,`--local`aredeprecated.Use`--location=global`instead.8.15.03、vue脚手架版本vue-V@vue/cli5.0.8三、创建vue项目1、创建名为vuetest的项目vuecreatevuetest选择Default([Vue2]babel,eslint)  2、切换到项目目录,启动项目cdvuetestnpmrunserve 3、使用浏览器预览 http://localh

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?

ruby - 使用带参数的 instance_eval 调用 proc?

我知道这行得通:proc=Proc.newdoputsself.hi+'world'endclassUsadefhi"Hello!"endendUsa.new.instance_eval&proc但是我想将参数传递给proc,所以我尝试了这个不起作用:proc=Proc.newdo|greeting|putsself.hi+greetingendclassUsadefhi"Hello!"endendUsa.new.instance_eval&proc,'world'#doesnotworkUsa.new.instance_eval&proc('world')#doesnotwork谁能

ruby-on-rails - 如何使用 Rails 中的 instance_eval 子句删除验证?

我想使用instance_eval增强现有类。原始定义包含验证,这需要存在某些字段,即:classDummytrueend现在我想使用instance_eval(或任何其他方法,真的)将其更改为可选:Dummy.instance_evaldo...end删除验证的正确语法是什么,因此该字段是可选的。我宁愿直接在模型层上这样做,而不是在Controller或View中进行奇怪的修改。instance_eval的使用并不是真正必需的,但据我所知,这通常是增强Rails类的最佳方式。编辑#1一般来说-原始类是gem的一部分,我不想fork它,也不想绑定(bind)到特定版本。一般原因并不重要

ruby-on-rails - RSpec any_instance 弃用 : how to fix it?

在我的Rails项目中,我使用rspec-mocks和any_instance但我想避免这个弃用消息:使用rspec-mocks的旧:should语法中的any_instance而不显式启用该语法已被弃用。使用新的:expect语法或明确启用:should。这是我的规范:describe(".create")doit'shouldreturnerrorwhen...'doUser.any_instance.stub(:save).and_return(false)post:create,user:{name:"foo",surname:"bar"},format::jsonexpect