self

Ruby Definition of Self

懵懂的女人 提交于 2019-11-26 18:12:38
问题 I was reading a Ruby book and came across this definition of the pseudo-variable self: self - receiver object of the current method Could someone break down that definition and explain what it means? I don't understand any of it. EDIT: I actually have a pretty good idea of what self is (and its applications) and I know how to search on Google. I was just wondering if someone could explain the definition I quoted. That specifically. 回答1: Ruby and other languages (such as Smalltalk and

Instance variable: self vs @

天涯浪子 提交于 2019-11-26 18:10:41
Here is some code: class Person def initialize(age) @age = age end def age @age end def age_difference_with(other_person) (self.age - other_person.age).abs end protected :age end What I want to know is the difference between using @age and self.age in age_difference_with method. Writing @age directly accesses the instance variable @age . Writing self.age tells the object to send itself the message age , which will usually return the instance variable @age — but could do any number of other things depending on how the age method is implemented in a given subclass. For example, you might have a

What is “self” used for in Swift?

无人久伴 提交于 2019-11-26 17:40:22
I am new to Swift and I'm wondering what self is used for and why. I have seen it in classes and structures but I really don't find them essential nor necessary to even mention them in my code. What are they used for and why? In what situations it's necessary to use it? I have been reading lots of questions and answers for this question but none of them fully answers my questions and they always tend to compare it with this as in Java, with which I'm not familiar whatsoever. You will also use self a lot when creating your extensions, example: extension Int { func square() -> Int { return self

When to access properties with 'self'

有些话、适合烂在心里 提交于 2019-11-26 16:43:51
I have read a number of questions on this site about this issue, I understand the following: self.property accesses the getter/setter method created manually or by @synthesize. Depending upon whether the property is declared as retain, copy etc. the retain count is modified correctly, for example a retained property, releases the previous value assigned the new value with 'retain' and increments the retain count by 1. Properties are usually declared with instance variables of the same name (can be different if you make the assignment manually). This is generally because the @synthesize

Is there a generic way for a function to reference itself?

孤人 提交于 2019-11-26 16:06:17
问题 I can access a python function's attribute inside of function itself by below code: def aa(): print aa.__name__ print aa.__hash__ # other simliar However, if above aa() function is a template for write other code, say bb() , I have to write: def bb(): print bb.__name__ print bb.__hash__ # other simliar Is there a "pointer" similar to the self argument in a class method so I could write code like this? def whatever(): print self.__name__ print self.__hash__ # other simliar I searched and found

What does “self” mean in javascript?

亡梦爱人 提交于 2019-11-26 15:59:27
问题 I read here that " self Refers to the current window or form ". Self does not seem to refer to the current form in this case: <form><input type="text" onkeyup="alert(self.foo.value)" name="foo"></form> However in this case it works (referring to the window): <form><input type="text" onkeyup="alert(self.document.forms[0].foo.value)" name="foo"></form> So when would you use the self DOM property over just window ? 回答1: For all windows, the self and window properties of a window object are

Why isn&#39;t self always needed in ruby / rails / activerecord?

浪子不回头ぞ 提交于 2019-11-26 15:37:25
问题 In testing a getter/setter pair in a rails model, I've found a good example of behavior I've always thought was odd and inconsistent. In this example I'm dealing with class Folder < ActiveRecord::Base . Folder belongs_to :parent, :class_name => 'Folder' On the getter method, if I use: def parent_name parent.name end ...or... def parent_name self.parent.name end ...the result is exactly the same, I get the name of the parent folder. However, in the getter method if I use... def parent_name=

How to keep track of class instances?

戏子无情 提交于 2019-11-26 13:23:57
Toward the end of a program I'm looking to load a specific variable from all the instances of a class into a dictionary. For example: class Foo(): __init__(self): x = {} foo1 = Foo() foo2 = Foo() foo...etc. Let's say the number of instances will vary and I want the x dict from each instance of Foo() loaded into a new dict. How would I do that? The examples I've seen in SO assume one already has the list of instances. One way to keep track of instances is with a class variable: class A(object): instances = [] def __init__(self, foo): self.foo = foo A.instances.append(self) At the end of the

How to avoid explicit &#39;self&#39; in Python?

馋奶兔 提交于 2019-11-26 13:07:06
I have been learning Python by following some pygame tutorials . Therein I found extensive use of the keyword self , and coming from a primarily Java background, I find that I keep forgetting to type self . For example, instead of self.rect.centerx I would type rect.centerx , because, to me, rect is already a member variable of the class. The Java parallel I can think of for this situation is having to prefix all references to member variables with this . Am I stuck prefixing all member variables with self , or is there a way to declare them that would allow me to avoid having to do so? Even

In Go is naming the receiver variable &#39;self&#39; misleading or good practice?

陌路散爱 提交于 2019-11-26 13:02:01
问题 I have seen a fair amount of blogs & videos on Go and as far as I recall, none of the authors use \'self\' or \'this\' for the receiver variable when writing methods. However there seems to be a number of questions on stack overflow that do this, and it got me thinking about if this is misleading to name the variable \'self\'? Reading the Spec for Method Sets does not provide any evidence either way (in my interpretation). I seem to recall finding somewhere that it was not really a self