草庐IT

self-joins

全部标签

ruby - 在类方法中使用和不使用 self 调用 Ruby 类方法有什么区别吗?

我有点好奇,下面这两种方法有什么区别吗?使用self在类方法中调用类方法classTestdefself.fooputs'Welcometoruby'enddefself.barself.fooendendTest.bar#欢迎使用ruby​​在没有自身的类方法中调用类方法classTestdefself.fooputs'Welcometoruby'enddefself.barfooendendTest.bar#欢迎使用ruby​​ 最佳答案 是的,有区别。但不是在你的例子中。但是,如果foo是一个private类方法,那么您的第一

ruby-on-rails - 有人可以向我解释 class << self 吗?

我是第一次接触Rails编程,在查看我下载的一些库的代码时,我偶尔会注意到代码:class我尝试在网上搜索解释,但是 最佳答案 在ruby中,class打开foo引用的对象的单例类.在Ruby中,每个对象都有一个与之关联的单例类,它只有一个实例。这个单例类包含特定于对象的行为,即单例方法。所以,class打开self的单例类.到底是什么self是,当然取决于您所处的环境。例如,在模块或类定义主体中,它是模块或类本身。如果您使用单例类的全部目的是定义单例方法,那么实际上有一个快捷方式:deffoo.bar.下面是一个示例,说明如何使用

ruby - 何时在模块的方法中使用 self

我的模块定义如下:moduleRG::Statsdefself.sum(a,args={})a.inject(0){|accum,i|accum+i}endend要使用这个方法,我只需要包含这个定义的文件,这样我就可以:RG::Stats.sum(array)还有RG::Stats.method(:sum)但是,如果我需要使用RG::Stats.instance_methods了解方法列表,我会得到一个空数组。这是因为我使用了self。如果我省略self,则RG::Stats.instance_methods会给出方法列表,但我无法再访问它们。问题是:如何在模块的方法定义中使用self

ruby - 何时在 Ruby 中使用 'self'

这个方法:defformat_stations_and_datefrom_station.titelize!iffrom_station.respond_to?(:titleize!)to_station.titleize!ifto_station.respond_to?(:titleize!)ifdate.respond_to?(:to_date)date=date.to_dateendend当date为nil时出现此错误:NoMethodError(Youhaveanilobjectwhenyoudidn'texpectit!Theerroroccurredwhileevaluat

ruby - class << self 中的类定义是做什么的?

我知道如何添加类方法和类行为usingself(eigenclass).但是,在阅读somesourcecode时,我看到了另一种用法:classLetterAvatarclass这是如何运作的?它有什么作用,什么时候应该使用它?什么是(可能更被认可的)替代方式来写这个? 最佳答案 我认为他们这样做是因为他们在其他任何地方都不需要这个类(class)。如果不打开单例类,流程将如下所示(假设原始代码中元类中定义的每个方法都将以self.为前缀):他们可以将Identity定义为classLetterAvatarclassIdentit

Ruby:使用 self 关键字从内部调用私有(private)方法

classMyClassdeftestputsmy_idputsself.my_idendprivatedefmy_id115endendm=MyClass.newm.test此脚本产生输出:115priv.rb:4:in`test':privatemethod`my_id'calledfor#(NoMethodError)frompriv.rb:15:in`'使用self关键字和不使用关键字从内部调用方法有什么区别?根据我的Delphi和C#经验:没有区别,self可以用来避免与局部变量的名称冲突,表示我想调用实例函数或引用实例变量. 最佳答案

ruby self.class.class_eval 或 singleton_class.class_eval

我这样做有什么区别classTdefinitializeself.class.class_evaldodeftestreturnself.class.object_idendendendend和classTdefinitializesingleton_class.class_evaldodeftestreturnself.class.object_idendendendend谢谢附言。塔斯回答说在这个例子中,singleton_class会为每一个新的对象返回不同的object_id,因为一个singleton_class只属于一个Object。但是IRB显示下一个1.9.2p180:

ruby - yield self 和 yield 的区别?

谁能帮我理解“yieldself”和“yield”的区别?classYieldFirstLastattr_accessor:first,:lastdefinitialize(first=nil,last=nil)@first=first@last=lastyieldselfifblock_given?enddefhelloputs"#{@first}#{@last}sayshello!"endend 最佳答案 在yieldself的情况下,self是传递给block的参数。使用简单的yield,不传递任何参数。self在这里并不特殊

ruby - ruby 模块 self.included 和 self.extended 行为记录在哪里?

我正在查看rubymixin博客文章,它说当一个模块包含在一个类中时,它的self.included()方法被调用。我的问题是,这种行为的正式记录在哪里?我似乎无法在ruby​​-docs.org网站或镐上找到它。 最佳答案 虽然它不在RubyDoc上出于某种原因,included实际上已被记录。在终端中运行riModule.included提供以下内容:included(othermod)Callbackinvokedwheneverthereceiverisincludedinanothermoduleorclass.Thiss

ruby-on-rails - Rails,activerecord : self[:attribute] vs self. 属性

在Rails中访问事件记录列/属性时,使用self[:attribute]与self.attribute有什么区别?这会影响getter和setter吗? 最佳答案 它们都只是获取属性的方法-它们都只是getter。self.attribtue是一个更“传统”的getter,而self[:attribute]基本上只是[]方法。在使用两者之间切换不会产生任何影响。我建议只使用self.attribute方法,因为它在语法上更好。但是,当其他内容覆盖self.attribute方法时,使用self[:attribute]会派上用场。例