jjzjj

instance_eval

全部标签

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 - 使用 class_eval 定义方法

我正在学习SaaS斯坦福类(class),尝试完成thisassignment的第5部分我很难理解这个概念,这就是我试图做的:classClassdefattr_accessor_with_history(attr_name)attr_name=attr_name.to_sattr_readerattr_nameattr_readerattr_name+'_history'class_eval%Q'{def#{attr_name}(a);#{attr_name}_history.push(a);end;}'endend我可能做错了各种各样的事情,阅读了Ruby之书关于元编程的章节,但我

ruby - 'eval' 应该是讨厌的吗?

我多次使用ruby​​的eval特性。但我听说有人说eval很讨厌。当被问及为什么以及如何使用时,我永远无法找到不使用它的令人信服的理由。他们真的很讨厌吗?如果是,以什么方式?eval有哪些可能的“更安全”选项? 最佳答案 如果您评估用户提交的或可修改的字符串,这无异于允许任意代码执行。想象一下,如果字符串包含对rm-rf/或类似的操作系统调用。也就是说,在您知道字符串受到适当约束的情况下,或者您的Ruby解释器被适本地沙箱化,或者理想情况下两者都存在的情况下,eval会非常强大。问题类似于SQLinjection,如果你很熟悉。这

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 - Ruby 中的 `eval` 何时合理?

"Is'eval'supposedtobenasty?"启发了这个:大多数人都认为eval不好,并且在大多数情况下有更优雅/更安全的替代品。所以我想问:如果eval经常被滥用,是否真的需要它作为一种语言特性?是不是弊大于利?就个人而言,我发现它唯一有用的地方是插入配置文件中提供的字符串。编辑:这个问题的目的是在eval是唯一或最佳解决方案时获得尽可能多的真实案例。所以请不要进入“语言是否应该限制程序员的创造力”的方向。Edit2:当我说eval时,我当然指的是eval字符串,而不是将ruby​​block传递给instance_eval或class_eval.

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谁能