Logo

dev-resources.site

for different kinds of informations.

"Sophie's World in Ruby"

Published at
12/23/2020
Categories
self
class
ruby
Author
kimiakamrava
Categories
3 categories in total
self
open
class
open
ruby
open
Author
12 person written this
kimiakamrava
open
"Sophie's World in Ruby"

Self in Ruby

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

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:

The current object depends on the context.

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?

self in instance method

class Alien
  def reflect
    self
 end
end
Enter fullscreen mode Exit fullscreen mode

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

self in class method

class Alien
  def self.reflect
    self
 end
end
Enter fullscreen mode Exit fullscreen mode

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

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

Class << self

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

photo_2020-12-23_11-40-18

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?

self Article's
30 articles in total
Favicon
Self hosted supabase setup with Authelia and Caddy
Favicon
Microsoft Certification Courses
Favicon
Hello World!
Favicon
Embracing Self-Care: Your Path to Wellness
Favicon
self, Self, and Self.self in Swift
Favicon
Day 14: Rust's Symphony of Generics and Traits – Crafting Code for the Real World 🌐🚀
Favicon
Day 13: Rust Enums – Unleashing the Power of Variants! 💪🦀
Favicon
Day 12: Navigating the Rustic Terrain of Struct Methods 🏞️
Favicon
Day 10: Rustic Riddles - Unleashing the Number Guessing Game 🎲
Favicon
Day 7: Unleashing Functions in Rust 🚀
Favicon
Day 5: Mastering Rust's Conditionals and Match Expressions
Favicon
Mindful Practices that help me get out of my own head and my way
Favicon
What Weightlifting Has Taught Me About Systems Engineering
Favicon
The Mindset Shift that Transform My Long-Term Outlook
Favicon
Embracing the Power of Being Second
Favicon
The 9-to-5 Scholar: Completing MIT OCW CSE with a Full time Job
Favicon
2022 retro, and what's up
Favicon
Find Prime Number in C++
Favicon
Top 6 Sites para Encontrar, Gerar, Criar e Compartilhar Memes
Favicon
Day-2 of Machine Learning
Favicon
The worst piece of life advice I ever received
Favicon
Como saber e validar o dígito verificador do RG (Registro Geral)
Favicon
Como descobrir qual a espécie de planta por foto?
Favicon
Orientação a objetos baseada em protótipos parte 2
Favicon
Top ways to become a self taught developer
Favicon
Why Polywork?
Favicon
Become a better front-end developer
Favicon
"Sophie's World in Ruby"
Favicon
Melhores Sites para Encontrar Estágios do Brasil
Favicon
Melhores Sites de Emprego do Brasil

Featured ones: