If you're trying to find yourself, please kindly exit this blog. If you are new to coding and can't quite figure out how to use self in object oriented Ruby, I might be able to help.
In programming, self becomes second nature the more you use it, but it can be somewhat confusing when you are first learning about it.
I often asked, "Do I use it meow?" "How about meow?" with minimal understanding of how and when to use it. When I first started, I just inserted and prayed it worked.
Here's how I made some sense of it:
- Hardwork and discipline
- Failing and getting back up again
- ... and now how to use self.
Let's say you have an object class FootballGame.
class FootballGame attr_accessor :hometeam, :awayteam, :date @@all =  def initialize(hometeam, awayteam, date) @hometeam = hometeam @awayteam = awayteam @date = date @away_team_score = 0 @home_team_score = 0 @@all << self end
Class in this example is your FootballGame class. It is very general. You can always count on any football game to have 4 quarters, 2 teams, a coin toss, cheerleaders, a bunch of terrible refs rooting against your home team, etc, etc.
Instance in this example is every game that has ever been or will be played. It's specific. Like that one game where Marshawn Lynch ran over a couple of Cardinals for registered seismic activity. A beastquake if you will.
We'll use that game to create a new instance of a FootballGame. You can initialize with the three attributes listed in the code above: hometeam, awayteam, date.
az_sea_01032015 = FootballGame.new("Cardinals", "Seahawks", "01/03/2015")
Awesome. We've created a new instance, named az_sea_01032015. Arizona is the home team, Seattle is the away team, and they played on 01/03/2015. Your naming convention should be something you recognize and any other programmer should be able to make sense of.
Let's take a look at some methods we've drawn up.
def home_td @home_team_score += 6 "We scored, yay!" end def away_td @away_team_score += 6 "They scored, boo." end def home_fg @home_team_score += 1 "And the field goal was good." end def away_fg @away_team_score += 1 "And the field goal was good." end
These are considered instance methods. How do we know?
- The singular @ symbol usually signifies instance variable.
- They are specific to the instance (Seahawks vs. Cardinals game).
- The information that you create will stay with that instance.
To call these methods we would just use the variable that we set when we created the instance.
az_sea_01032015.home_td => "We scored, yay!" az_sea_01032015.home_fg => "And the field goal was good." #=> @home_team_score = 7 #Although the Cardinals may have scored first...SPOILER alert, Seahawks won!
Let's look at some more methods.
def self.all @@all end def self.intro "Game on! - Carrie Underwood" end def self.coin_toss ["heads", "tails"].sample end
These are considered class methods. How do we know?
- The period in the method name.
- Two @@ symbols.
- These methods can be called on the class. It pertains to every instance that can be created.
- Why should we use self. here? I won't say, "Dan-Vy(my name is Dan-Vy if you didn't catch that earlier) is going to the park." I'll just tell you, "I'm going to the park." I am already here in front of you, just like how you're already in your FootballClass method.
To call on your class methods:
FootballGame.all => [#<FootballGame:0x00007fccdf954d50 @away_team_score=6, @awayteam="Seahawks", @date="10/10/18", @home_team_score=12, @hometeam="Packers">, #<FootballGame:0x00007fcce0148200 @away_team_score=0, @awayteam="Seahawks", @date="01/03/2015", @home_team_score=7, @hometeam="Cardinals">]
Here we called FootballGame.all(In our code, we recognize it as the self.all method). It lists every game(instance) that we have created in FootballGame class. Nice.
FootballGame.intro => "Game on! - Carrie Underwood" FootballGame.coin_toss => "tails"
These methods are here to drive in your understanding of class. These are general methods that can be called on every game, not just one.
Hope this helped! I'll end this the way a certain QB finishes all of his interviews with: "Go Hawks." I wish you the best in your coding journey!