Use of Objects

Nicky Liu
4 min readSep 10, 2018

--

Objects are the cornerstone of Ruby as a programming language. Almost everything is an object, and objects are fairly easy to handle. They have a base class template that lets you create a new object, allows an object to have attributes, and allows for methods to be called on them.

The following is an example of a car class

class Car
attr_reader :color, :name
@@all = []
def initialize(name, color)
@name = name
@color = color
@@all << self
end
def self.all
@@all
end
def drive
puts "#{self.name} goes vrooming off."
end
def self.find_car_by_name(name)
self.all.select do |car|
car.name == name
end
end
end

An object is created with the ObjectClass.new method (which often takes attributes)

car1 = Car.new("Lightning McQueen", "red")

This creates an instance of the class car, a car object. It’s attributes can be called

car.name ==> "Lightning McQueen"

and methods can be called on them (such as the aforementioned method attributes, but also custom defined methods)

car1.drive ==> "Lightning McQueen goes vrooming off."

Class methods can also be defined within the class that are run on the entire class itself rather then an instance of the class.

car1 = Car.new("Lightning McQueen", "red")
car2 = Car.new("Doc Hudson", "blue")
Cars.find_car_by_name("Lightning McQueen") ==> [car1]

One of the uses of objects is to make it easier to store and retrieve information. If you were to play a trading card game without using objects, each time a card is dealt you would need to input all of the necessary information.

hand = []
hand << [name, hp, attack, cost, rarity]

You would end up with a hand full of arrays (or hashes if you choose to go that route). Then if that card is moved from your hand to somewhere else, all of that information must again be completely transferred over.

field = []
field << [name, hp, attack, cost, rarity]

Furthermore, if the card company decided to add an extra stat, called defense, after attack

[name, hp, attack, defense, cost, rarity]

any code that tried to retrieve the cards cost before would now instead retrieve its defense, and everything will have to be re-written.

But if you use objects, than after the object is created once with all of its attributes, only the object itself needs to be passed around.

card1 = Card.new(name, hp, attack, cost, rarity)
hand << card1
field << card1

And if a new stat is added, only the initialize method needs to be changed to include the defense, and everything else will work the same, since attributes are accessed not by position, but by attribute name.

card1.name ==> name

Objects are especially useful when representing things inside video games, as video game enemies and allies alike often are one singular thing that has attributes such as name, attack, hp, abilities, etc, which can perfectly be represented by objects. For example, in Hearthstone there is a card:

Whenever this card is not killed in a single attack, it will summon an extra copy. However, the copies that this card summons retain the stats at the time of summoning. If the ooze was 5/2, it summons another 5/2 ooze instead of a new 2/4 one. The best way to do this is to simply have had this card as an object with a hp and attack attribute that are tracked within the object, than when it splits instantiate an object with the exact same stats.

In MOBAs such as league of Legends, you control a character with stats, and a position on the map. The character would be an object having attributes like HP, attack, armor, and items, but also location coordinates which would constantly update depending on where you are on the map, and which determine whether or not an attack made contact with you. Without an object having constantly updating x, y coordinates, this would be fairly difficult to keep track of.

Objects are great for keeping track of something that contains a variety of attributes, especially if they can change and need those values to be memorized. Whether it is an account that keeps track of level and win/loss, a character that grows and gets stronger, or cards that can have their stats changed, objects are great for representing almost anything in video games.

--

--

No responses yet