"Sophie's World in Ruby"
Tags: self, class, ruby
Published: 2020-12-23 19:54:56

<h1>Self in Ruby</h1>
If You are a beginner self can be a little bit confusing, so I decided to do a comic out of self. It's like the book "Sophie's World" where she wanted to understand who she is and where did she come from?
That being said let's go way back to understand self.
we know everything created by Creator Ruby is an object and this means all the codes or "storylines" "belongs" to some object. Just like in real life there will be many objects with many storylines.
when there is so much data how can we find the owner of the code or the object?
That is where Ruby Keyword comes and gives us access to the current object!

![photo_2020-12-23_01-10-30](https://dev-to-uploads.s3.amazonaws.com/i/7nlpzdjzuqrkly59ttya.jpg)
 



sorry to say there is not much philosophy behind it.But you will have the power to type in self and ruby knows which object you are talking about. The tricky part starts right now:
<h2>The current object depends on the context.</h2>
before we start with the examples =>

!Hint: if everything is an object even classes are objects...then we can use self when defining Classes, Methods, Modules.
In the following Examples, there will be Aliens standing in front of the mirror trying to figure out who they really are?

<h2>self in instance method</h2>
  ```
class Alien
  def reflect
    self
 end
end
```
we have sir biboo a polite alien.
biboo = Alien.new
biboo.reflect == biboo
 
so Mr biboo will see himself in the mirror!

![photo_2020-12-23_01-10-16](https://dev-to-uploads.s3.amazonaws.com/i/pjl0bm7wuk7y1jp8qakq.jpg)
 



<h2>self in class method</h2>
 ```
class Alien
  def self.reflect
    self
 end
end
```
Alien.reflect == Alien

Here the Object is not Mr biboo but the class Alien, which i see as a factory object making lots of Aliens for us:D.

![photo_2020-12-23_11-40-13](https://dev-to-uploads.s3.amazonaws.com/i/70eg77yx3ekoctiwx7px.jpg)
 

!Hint: A reminder that it works the same inside of modules.

<h2> Class << self </h2>
Before this Example once more we were told everything is an object in ruby world right? so the Class of any class is Class, checkout this blog ( https://dev.to/samuelfaure/explaining-ruby-s-singleton-class-eigenclass-to-confused-beginners-cep ) that blew my mind!
```
#We can create ExampleObject like this :
class ExampleObject
end

# Or like this :
ExampleObject = Class.new

ExampleObject.new.class # => ExampleObject
ExampleObject.class # => Class
Class.class # => Class
ExampleObject.class.class.class.class # => Class
```
According to the Example:
our Alien will say Hi in another language

```
class ExampleAlien
  def printHello
    puts 'Hello'
  end
end

Alien1 = ExampleAlien.new
Alien2 = ExampleAlien.new

class << Alien2
  def printHello
    puts 'Aloha'
  end
end

Alien1.printHello # => 'Hello'
Alien2.printHello # => 'Aloha'
```

![photo_2020-12-23_11-40-18](https://dev-to-uploads.s3.amazonaws.com/i/0g1fb5n7age4vc6el53x.jpg)
 

A list of other-self uses:
 •Define class-level methods
 • Use an instance method when you have a local variable of the same name
 • Returning Self (builder pattern)
 • Debugging
 • Comparing objects (==)
 • Default receiver of method calls

Hope you had fun understanding self. so Who are you really?