Browse files

adding lesson notes for self

  • Loading branch information...
danman01 committed Jul 9, 2018
1 parent b19ff73 commit cbde68c807140a40895fb63a43bc4821137748e5
Showing with 41 additions and 0 deletions.
  1. +41 −0 ruby-object-self/
@@ -0,0 +1,41 @@
## LESSON: [ruby-object-self](




Developers should be able to:

- Determine runtime context using `self`.
- Invoke a getter or setter method using `self`.'


**Vocab Word**: Definition of word


- Just like JavaScript's `this` keyword, `self` in Ruby is _a reference to the runtime context of your program_. We can use `self` instead of referring to particular instances to get or set data on these objects. Think of `self` as a pronoun for any object in our system.

- Just like pronouns, `self` changes depending on the context in which it is used. At times `self` is going to refer to a particular object. Later, `self` could refer to a different object. In fact, at some times in a running program `self` may point to a class.

- At every point in time when your program is running there is one and only one class or object that `self` is 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 `self` would point to the instance that invoked the method, or you could be inside a class definition and `self` would point to the class itself.

- `self` will point to one of three runtime contexts: _global context, object context,_ or _class context_.

#### Global
- For all methods invoked without a class or object `self` will be an instance of the `Object` class, _main_.

#### Object

- For all methods invoked on an object `self` will 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_name` will 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

0 comments on commit cbde68c

Please sign in to comment.