Site icon Web Design Ledger

My Journey Of Learning Programming Through Flatiron School #15

My name is Mason Ellwood, and I’m currently working on Flatiron School’s Online Full Stack Web Development Program. Each week, I’ll be writing about my experience, what I’m learning, and tips on learning to code.

Last post I got to introduce the concept of Object Oriented Programming with Ruby. This concept, at least for me, has been a major stepping stone to better understanding Ruby as a language and the power that it has in the development space. But with this concept came a plethora of new terms, keywords, and thought processes with it.

Just like in the last post, we will be continuing to talk about instances and the return of the instance variable that has been associated with that object.

“I thought of objects being like biological cells or individual computers on a network, only able to communicate with messages” – Alan Key

Instance Methods

Something that we will extensively is DOT notation. DOT notation allows us to send objects messages asking to perform an operation or task through a DOT notation syntax.

The object_id method simply tells the object identifier where it is located in your computer’s memory. In DOT notation, we call the object that received the method message the “receiver” and we call the method the “messenger”.

Adding method definitions to the class body enables you to add method behaviors to that instance of the class. We call the method defined within the object class instance methods because they are methods that belong to any instance of the class.

By defining the instance method of #bark within the Dog class, #bark becomes a method of all instances of Dog. This means that all dogs created with the Dog class will bark. In the same manner, instance methods are not globally evocable like in procedural methods.

Instance Variables

buddy = “hey, buddies”

In the above example “buddy” is known as a local variable, this is because it can only be accessed in a specific local environment. A local variable that is defined inside one method, cannot be accessed by a different or another method. To get around this we use instance variables inside Ruby classes.

Unlike a local variable, an instance variable is a variable that is accessible in any instance method in a particular instance of a class.

name= or “name equals” takes in an argument of a dog’s name and sets arguments equal to the variable this_dog_name. Then reports reading the #name in the second method. BUT it breaks. Don’t forget that local variables have a local scope, that means that it cannot be accessed outside of the method in which it is defined. To define an instance method we preface the variable with an @ symbol. Instance methods are bound to an instance of a class remember. Updating our class to this syntax, we will then be able to call jojo.name.

Because @the_dog_name is now an instance method it now has access to the name set in the name= method.

Awesome! We covered some good ground today. Let me know if you have any questions and ill try my best to respond. Thank you for reading.

 

Exit mobile version