Developers should be able to:
- Determine runtime context using
- Invoke a getter or setter method using
Vocab Word: Definition of word
selfin Ruby is a reference to the runtime context of your program. We can use
selfinstead of referring to particular instances to get or set data on these objects. Think of
selfas a pronoun for any object in our system.
Just like pronouns,
selfchanges depending on the context in which it is used. At times
selfis going to refer to a particular object. Later,
selfcould refer to a different object. In fact, at some times in a running program
selfmay point to a class.
At every point in time when your program is running there is one and only one class or object that
selfis referencing or pointing to.
At any time during the life of your program the context may change. You may be running code inside of an instance method and
selfwould point to the instance that invoked the method, or you could be inside a class definition and
selfwould point to the class itself.
selfwill point to one of three runtime contexts: global context, object context, or class context.
- For all methods invoked without a class or object
selfwill be an instance of the
- For all methods invoked on an object
selfwill point to that object.
Getters and Setters
- implied self if used in getters (you don't need to write it, ruby looks up the chain of available variables, and
self.property_namewill be there
- you must use self inside setters. This is because, without
self.property_name, ruby would interpret it as assigning a value to a variable: `property_name = new value