草庐IT

Ruby block 、procs 和 instance_eval

全部标签

ruby - 我如何使用 class_eval?

我不明白class_eval。classModuledefattr_(*syms)syms.eachdo|sym|class_eval%{def#{sym}=(val)@#{sym}=valend}endendend%是什么意思?class_eval有什么作用?(val)来自哪里? 最佳答案 简短的回答是:您可能希望避免像这样使用class_eval。这是对您的代码的解释:%{hello}只是在Ruby中编写字符串文字的另一种方式,无需担心在字符串中转义双引号或单引号:%{hello"world"}=="hello\"world\"

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 - 如何从 Proc 对象中提取代码?

给定一个Proc对象,是否可以查看其中的代码?例如:p=Proc.new{test=0}我需要的是通过某种方式从已创建的Proc对象中获取字符串“test=0”。 最佳答案 您可以使用ruby2ruby图书馆:>>#testedwith1.8.7>>require"parse_tree"=>true>>require"ruby2ruby"=>true>>require"parse_tree_extensions"=>true>>p=Proc.new{test=0}>>p.to_ruby=>"proc{test=0}"您还可以将此过程

Ruby 方法、Proc 和 block 混淆

关于Ruby的方法、过程和block,我有几个问题让我觉得很奇怪。与其说是语法或功能,不如说是决策背后的逻辑。问题1:为什么可以将block传递给方法(例如each)但不能将它们分配给变量?我知道您可以在过程中传递它们,即p=Proc.new{...}(使用&p访问),但它不会让程序员通过这些方法很有意义。问题2:为什么方法和程序之间存在差异?例如,我可以通过以下两种方式完成定义一个函数并调用该函数的任务:defsquare(x)x**2endsquare(3)=>9或square=lambda{|x|x**2}square.call(3)=>9为什么要区分?例如,在Python中,以

ruby - Proc 和 Lambda 之间的区别

Ruby在通过Proc.new和lambda(或1.9中的->()运算符)创建的Proc之间存在差异。似乎非lambdaProcs将跨block参数传递一个数组;通过lambda创建的过程不会。p=Proc.new{|a,b|a+b}p[[1,2]]#=>3l=lambda{|a,b|a+b}l[[1,2]]#=>ArgumentError:wrongnumberofarguments(1for2)有人了解这种行为背后的动机吗? 最佳答案 lambda和非lambdaProc之间有两个主要区别:就像方法一样,lambda从自身返回,

ruby - 如何在 Ruby 中编码 lambda (Proc)?

乔范戴克askedtheRubymailinglist:Hi,InRuby,Iguessyoucan'tmarshalalambda/procobject,right?Isthatpossibleinlisporotherlanguages?WhatIwastryingtodo:l=lamda{...}Bj.submit"/path/to/ruby/program",:stdin=>Marshal.dump(l)So,I'msendingBackgroundJobalambdaobject,whichcontainsthecontext/codeforwhattodo.But,gues

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 - 使用 Proc#call 时为自己提供值(value)

在Ruby中使用Proc#call调用lambda函数时,self总是以定义函数时的值结束,而不是调用函数时的值,例如:$p=lambda{self}classDummydeftest$p.callendendd=Dummy.new>d.test=>main调用test返回main,当我打算返回的是#-Dummy的实例,这是self的值在我调用$p的代码中.在Javascript中,我只是将我想成为“被调用者”的对象作为第一个参数传递给call。.Ruby中是否有这样的功能,允许我设置任意对象,或者至少设置当前值self,作为self的新值当我调用Proc?

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之书关于元编程的章节,但我