Classes in Ruby
Classes are abstractions in object-oriented programming languages like Ruby and used to create any construct that contains data and functions that serve a purpose as a single unit. Some classes are built in, such as strings (raw text) and arrays (a primitive form of spreadsheet) that not only have the payload data itself, but have methods that perform functions like reversing the data or providing its length. However there is only so much that can be done with them, so it is necessary to be able to create unique classes and languages like Ruby have the capabilities for that.
Suppose you want to write a program that processes scores from the World Cup. The most obvious class you would have to make is a game class so that you can create instances of it for each game that is played. There's a lot of information that a program could capture, but for this example let's keep it simple.
The first part that needs to be done is something that instantiates the class for each game. Ruby calls it the initializer method. Each game is a home team and a visiting team, so let's start with the basics:
class Game def initialize(home,away) @home=home @away=away @home_score=[] @away_score=[] end end
Notice how there's variable with an '@' character in front of it. These are instance variable that are kept with each instance of the class so that each instance can have it's own value for each of the variables.
The next thing you may want to add is a method that adds a goal that is scored. For instance:
def goal(time,team,player) if team==@home @home_score.push([time,player]) else @away_score.push([time,player]) end end
We also need methods that return the score and the outcome:
def score(team) if team==@home return @home_score.length else return @away_score.length end end def winner() if @home_score.length > @away_score.length return @home elsif @home_score.length < @away_score.length return @away else return "tie" end end
Putting all of this together with an object instance declaration we get:
class Game def initialize(home,away) @home=home @away=away @home_score=[] @away_score=[] end def goal(time,team,player) if team==@home @home_score.push([time,player]) else @away_score.push([time,player]) end end def score(team) if team==@home return @home_score.length else return @away_score.length end end def winner() if @home_score.length > @away_score.length return @home elsif @home_score.length < @away_score.length return @away else return "tie" end end end game1 = Game.new("Brazil","Croatia") game1.goal(11,"Croatia","Marcelo") puts game1.score("Brazil") + "-" game1.score("Croatia") game1.goal(29, "Brazil","Neymar") puts game1.score("Brazil") + "-" game1.score("Croatia") game1.goal(71, "Brazil","Neymar") puts game1.score("Brazil") + "-" game1.score("Croatia") game1.goal(91, "Brazil","Oscar") puts game1.winner()
The output of this code should be the following as the score gets printed after each goal:
0-1 1-1 2-1 3-1