Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.md

README.md

LESSON: ruby-object-self


2018-07-06

OBJECTIVES

Developers should be able to:

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

VOCABULARY

Vocab Word: Definition of word

NOTES

  • 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