...

/

Object Scope and Self

Object Scope and Self

This lesson discusses the scope of an object and the usage of the keyword "self".

Remember how we said that when Ruby finds a method call she then deviates from the normal control flow which goes from top to bottom? Instead, she jumps into the method body.

We also said that this method body has its own scope, like a shiny, new room where local variables from other scopes are not visible. Instead, it has its own local variables, some of which might be defined via the method’s arguments.

And we also said that inside an object’s method all *instance variables of this object, and all other methods of this object are also visible.

We also may have a look at that mysterious top-level object that Ruby enters when she starts executing a program, or IRB.

We are now finally ready to put all these things together a little more, and introduce a new keyword: self.

The Object Scope

In fact, in Ruby, there are even more ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy