Ruby: Object Lifecycle (2)
By: Shirlen Detablan
Tags: Super Smash Bros, Object Oriented, Programming, Ruby

Photo by Jason D on Unsplash

Continuing the previous post about the object lifecycle in Ruby. I’ll be going over a basic gist of variables and objects with one of the many methods that can be used.

Variables

We have four types of variables in Ruby: class, instance, local, and global variables. To simplify the difference between them, I’m going to share this chart as it helped me understand the four different types.

One way to keep track of any variable is by storing their behaviour in a class variable. As explained above, the class variable can be accessed and shared by all descendants inside the class and will be changed in every instance. They are also sometimes known as static variables or static methods. An instance variable can also be accessed from any instance of a class within a method but will only have limited access to an instance of a class.

Note that when a class variable or instance variable is uninitialized then it will return an error or a nil value. Always make sure that the variables that you’ll be sharing across your class have been initialized!

Objects

Everything in Ruby is an object. Even the methods we assign to an object are available to all classes (unless we explicitly tell Ruby that it isn’t). An object can even call on itself. In the previous post, we created new instances of an object and gave it unique attributes. You can also destroy an instance of an object given certain conditions. To demonstrate that, let’s use an example:

class Kirby

attr_accessor :hp :damage def initialize(hp, damage)

@hp = hp

@damage = damage

end def uses_smash_ultimate

puts self

end

end object = Kirby.new(HP=100, Damage=10) => :uses_smash_ultimate

What would it look like in the destruction process?

class Kirby

attr_accessor :hp :damage def initialize(hp, damage)

@hp = hp

@damage = damage

end def uses_smash_ultimate

puts self

end def kirby_is_still_alive

if self.hp > 0

end

end def ko

self.destroy

end def enemy_defeats_kirby

hp = 0

end def kirby_loses

if enemy_defeats_kirby

ko

else

kirby_is_still_alive

end

end end

In this example, we’ve defined that Kirby’s defeat is conditional. An instance is destroyed if the enemy defeats Kirby by leaving them at 0 HP otherwise they still live.

One of the methods we can use in Ruby is “.send”. Difference with the call method is that it doesn’t define an object and only gives it a message. Send, however, does define an object and can also be used in private methods. Ruby has made it convenient to pass arguments to these methods using “.send”. Furthermore, the arguments that you send right after the first one becomes an argument that will get passed down to the method.

class Unlockable

attr_accessor :name def initialize(unlockable, name)

@unlockable = unlockable

@name = name

end def completed_challenge(name)

"New challenger, #{name}!"

end



def unlocked(unlocked, argument = nil)

if unlockable == new_challenger

@unlockable.send(unlocked)

else

unlockable == nil

end

end end

All methods are public by default unless you explicitly tell Ruby that they’re private. Any instance method after private become private methods. But with the “.send” method, we can access private methods.

class Unlockable attr_accessor :name

private :name def initialize(unlockable, name)

@unlockable = unlockable

@name = name

end def unlocked(unlocked, argument = nil)

if unlockable == completed_challenge

@unlockable.send(unlocked)

else

unlockable == nil

end

end private def completed_challenge(name)

"You got #{name}!"

end end object1 = Unlockable.new("Challenger", "Sephiroth") # "You got Sephiroth!"

While it may be odd to declare the name in a private method when it can be accessed by “@” , it can be useful to modify it later on without having to replace class instances.

I hope this helps you understand objects and variables more. While I haven’t covered most of it, I will be continuing with a part 3 in the future. Have a wonderful holiday weekend and keep on coding!

_____________________________________________________________

Social Links

LinkedIn

GitHub

Twitter

Instagram

YouTube