jjzjj

instance

全部标签

ruby - 模块嵌套在 instance_eval/exec 或 module_eval/exec 中

我在尝试回答this时想到了这个问题.以下是预期的行为:moduleApModule.nestingend#=>[A]但是以下内容:A.instance_eval{pModule.nesting}A.instance_exec{pModule.nesting}A.module_eval{pModule.nesting}A.module_exec{pModule.nesting}全部返回[]。为什么这些不能像上面那样工作?附加问题Muistooshort提出了一个有趣的观点。如果这是正确的,那么Module.nesting将是依赖于文字上下文的方法和变量之一,例如Method#sourc

ruby - 以元编程方式定义采用关键字参数的 Ruby 方法?

Struct让我创建一个新类,它接受参数并具有一些很好的语义。但是,参数不是必需的,它们的顺序需要引用定义:Point=Struct.new(:x,:y)Point.new(111,222)#=>Point.new(111)#=>我想要类似于Struct的东西,但它使用关键字参数代替:Point=StricterStruct.new(:x,:y)Point.new(x:111,y:222)#=>Point.new(x:111)#=>ArgumentError这可能看起来像这样:moduleStricterStructdefself.new(*attributes)klass=Class

ruby - 如何像 instance_eval 方法那样在一个 block 中改变 self 呢?

instance_eval方法在其block中改变自身,例如:classD;endd=D.newd.instance_evaldoputsself#printsomethinglike#,not'main'!end如果我们自己定义一个方法(或任何其他方法(除了instance_eval)需要一个block),当打印self时,我们将得到'main',这与instance_eval方法不同。例如:[1].eachdo|e|putsself#print'main'end我如何定义一个像instance_eval这样的方法(需要一个block)?提前致谢。 最佳答

ruby - 在 Ruby 中调用另一个类中的一个类中的方法

我想知道如何在另一个类的一个类的实例中调用一个方法。这是我想出来的classClassAdefmethodreturn"Thisisamethod_from_class_A"endendclassClassBdefinitialize@method_from_class_A=instance.methodenddefmethod_calls_method@method_from_class_Aendendinstance=ClassA.newinstance2=ClassB.newputsinstance2.method_calls_method但是我得到这个错误:Testing.rb

Ruby 在没有显式包含声明的情况下在子类上执行 self.included(base) 方法?

我有许多继承自一个父类(superclass)的类。作为模块定义的父类(superclass)。模块内部是一个设置一些实例变量的self.included(base)方法。所以像这样:moduleMyModuledefself.included(base)base.instance_variable_set("@my_instance_variable",{})endendclassMySuperClassincludeMyModuleendclassClassA除非我明确地将MyModule包含在ClassA和ClassB中,否则我的实例变量将不会在这两个类中设置。有没有办法确保在每

ruby - 传递给 `instance_exec` 时如何执行 proc

问题的灵感来自thisone.Proc::new有一个选项可以在方法内部没有block的情况下调用:Proc::newmaybecalledwithoutablockonlywithinamethodwithanattachedblock,inwhichcasethatblockisconvertedtotheProcobject.当proc/lambda实例作为代码块传递时,将创建Proc的新实例:Proc.singleton_class.prepend(Module.newdodefnew(*args,&cb)puts"PROC#{[block_given?,cb,*args].i

ruby - 模块的实例变量是否在类与 mixin 之间共享?

我想知道Ruby模块的实例变量在多个类中的行为如何“混合”它。我写了一个示例代码来测试它:#HereisamoduleIcreatedwithoneinstancevariableandtwoinstancemethods.moduleSharedVar@color='red'defchange_color(new_color)@color=new_colorenddefshow_colorputs@colorendendclassExample1includeSharedVardefinitialize(name)@name=nameendendclassExample2includ

ruby : how to prevent modification of an array instance variable through an attribute reader

抱歉这个菜鸟问题...假设我们有:classTestMeattr_reader:arraydefinitialize@array=(1..10).to_aend结束然后可以这样做:>>a=TestMe.new=>#>>a.array.map!&:to_s=>["1","2","3","4","5","6","7","8","9","10"]>>a.array=>["1","2","3","4","5","6","7","8","9","10"]这显然不利于封装,不是吗?有什么方法可以快速保护数组变量不被更改吗?...或者每当我的实例变量具有“破坏性”方法时,我是否需要实现一个深拷贝读取

ruby - 为什么 instance_eval 对 Proc 成功但对 Lambda 不成功?

我有以下类(class):classUsercode1=Proc.new{}code2=lambda{}define_method:testdoself.class.instance_eval&code1self.class.instance_eval&code2endendUser.new.test为什么第二个instance_eval失败并出现错误数量的参数(1代表0)错误? 最佳答案 instance_eval正在将self(User)生成给lambda。Lambda对其参数有特殊要求-方法也是如此-如果参数太少/太多,将引发

ruby - Rspec any_instance.stub 为 nil :NilClass exception 引发未定义的方法 `any_instance_recorder_for'

这是我正在测试的包含在Foo.rb中的类:classFoodefbarreturn2endend这是Foo_spec.rb中包含的我的测试:require"./Foo.rb"describe"Foo"dobefore(:all)doputs"#{Foo==nil}"Foo.any_instance.stub(:bar).and_return(1)endit"shouldpassthis"dof=Foo.newf.bar.shouldeq1endend我得到以下输出:falseFFailures:1)FooShouldpassthisFailure/Error:Foo.any_insta