ga-wdi-boston / ruby-object-self

Other
1 stars 141 forks source link

Global code snippet should be above it's current location #18

Closed BenGitsCode closed 7 years ago

BenGitsCode commented 8 years ago
### Global

For all methods invoked without a class or object `self` will be an instance of the Object class, *main*.

```ruby
puts "In the global scope, self is #{self}"

def example
  puts "In a method attached to the global scope, self is #{self}."
end
raq929 commented 7 years ago

@BenGitsCode In the future, can you do these as a diff? 😄

BenGitsCode commented 7 years ago

I think this was supposed to be like this: Though looking at it, I'm not sure why. I believe I opened this during the last delivery, but I think you delivered it...so if we both don't know what I'm talking about, it may not be an issue.

+### Global

+For all methods invoked without a class or object `self` will be an instance of the Object class, +*main*.

+```ruby
+ puts "In the global scope, self is #{self}"

+def example
+  puts "In a method attached to the global scope, self is #{self}."
+end

+example
+```
## Context

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 be a particular object. Later, `self` could be a different object. In fact, at some times in a running program `self` may be 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*.

- ```ruby
- puts "In the global scope, self is #{self}"

- def example
-  puts "In a method attached to the global scope, self is #{self}."
- end

- example
-```

That makes the most sense of this issue, but I'm not sure that change makes sense / seems necessary.

raq929 commented 7 years ago

OK, that makes sense now. I think I want to leave it where it is for now. Let's reopen if it seems like an issue in delivery for 016.