Ruby: Object Lifecycle (1)
By: Shirlen Detablan
Tags: Object Lifecycle, Object Oriented, Programming Languages, Programming, Ruby

Photo by Jason D on Unsplash

After going through a mock technical interview through Skilled, I realized then that there was a lot missing in my fundamental knowledge of basic programming. I’m going to start writing out what I currently know now and look back to it in the future. To start off is the object lifecycle in Ruby. One of my main struggles starting out were methods with undefined variables. This can be solved and once defined it can be utilized for later use.

What is a class?

In object oriented programming, a class is essentially a template with data that contains the information and behaviour. In a class, we can instantiate a new object and give it attributes. We can even instantiate multiple objects within a single class with new attributes as well.

Classes and Objects

These few lines of code in Ruby allows us to create a new class:

class Videogame

end

Creating a new class in Ruby essentially creates a new data type for us to add attributes into. Attributes are information that we provide for the given example of our new class. These attributes can be written like so:

class Videogame

attr_accessor :name, :studio, :genre, :price

end

We can then see that the attributes of a videogame contain a name, studio, genre, and platform. Now that we’ve told Ruby that information, we can then create objects that represent individual instances of our Videogame. We want to create a new Videogame and store that data-type. We’ve defined that it’ll have these attributes so now we can create a new object for it.

class Videogame

attr_accessor :name, :studio, :genre, :price

end videogame1 = Videogame.new()

videogame1.name = "Cyberpunk 2077"

videogame1.studio = "CD Projekt Red"

videogame1.genre = "RPG"

videogame1.price = "$60"

An object is basically an instance of a class. So now the videogame1 object is inside our program that we can interact with by giving it information. If you enter the object with it’s attribute in the console you can then see the following information. We’ll create an additional object inside that class with new attributes as well and also receive what we’ve entered:

class Videogame

attr_accessor :name, :studio, :genre, :price

end videogame1 = Videogame.new()

videogame1.name = "Cyberpunk 2077"

videogame1.studio = "CD Projekt Red"

videogame1.genre = "RPG"

videogame1.price = "$60" # puts videogame1.name

# "Cyberpunk 2077" videogame2 = Videogame.new()

videogame2.name = "Sims 4"

videogame2.studio = "Electronic Arts"

videogame2.genre = "Sandbox"

videogame1.price = "$60" # puts videogame2.genre

# "Sandbox"

Next, we can call on a new object using the initialize method.

class Videogame

attr_accessor :name, :studio, :genre, :price def initialize(name, studio, genre)

@name = name

@studio = studio

@genre = genre

@platform = platform

end end videogame1 = Videogame.new("Cyberpunk 2077", "CD Projekt Red", "RPG", "$60")

In this example, we’re telling Ruby that these are attributes that the user will be passing parameters into. The attributes can be anything that the user enters that can also be returned to us when called on. Notice how the videogame1 object is now all in a single line compared to before?

Next we can add a new method to our class.

class Videogame

attr_accessor :name, :studio, :genre def initialize(name, studio, genre)

@name = name

@studio = studio

@genre = genre

end def on_sale

# will return a boolean value

end end

We can define what will return true or false for our object. Let’s say, if a videogame is an RPG then it will return true and if not then it is false.

class Videogame

attr_accessor :name, :studio, :genre, :price def initialize(name, studio, genre, :price)

@name = name

@studio = studio

@genre = genre

@price = price

end def rpg_game

if @genre == "rpg"

true

else

false

end

end end

We’re comparing an object’s attribute here to see if a certain videogame is an RPG genre. We know that Cyberpunk 2077 is an RPG so therefore it will return true. However, if we were to have another object like videogame3.genre of “Sports” then that will return false since it is not an RPG game.

Debugging Errors

Let’s look at this code for example and see what’s wrong with it:

class Videogame

attr_accessor :name, :studio, :genre, :price def initialize(name, studio, genre, :price)

@name = name

@studio = studio

@genre = genre

@price = price

end def rpg_game

if @genre == "rpg"

true

else

false

end

end def indie_game

if @studio == indie

true

else

false

end

end end

If we run this code it will return an error because we haven’t defined a method that Ruby would understand whether a studio is an independent one or a corporate run studio. In the past when making something like this, it would fall on me to add additional attributes to define whether a studio is independently run or not. That way if we were to use “indie”, then it can return a boolean value. Otherwise, we’ll encounter errors since it was never defined.

On my next post, I’ll write about key concepts of Object Oriented Programming with Ruby and some pointers to avoid mistakes like I have.

_____________________________________________________________

Social Links

LinkedIn

GitHub

Twitter

Instagram

YouTube