Photo by Emile Perron on Unsplash

Beginning Object Orientation in Ruby, and Creating A Class

So you have some fundamental understanding of ruby, GREAT! Ruby is what we call an Object Oriented Program(OOP), which means most of what you’ve seen so far is an object. The point of Object Oriented Programming is to teach Ruby what any specific object is, what it does, and how to access that object. What makes objects objects is it has 2 key properties: being that the object has attributes and it has methods. What assigns these objects attributes and methods is called a Class, which in itself is also an object! That can be very confusing…

Classes can be seen as “ideas” a concept of a whole, while objects can be considered “things”, and these things are expressions or instances of a class. In Ruby some classes are already apart of the package, meaning you could do simple calculations or text transformations without writing a single class yourself! Below is a link to an amazing resource on all the methods you can call on objects in ruby.

Our goal in more complex programming is to create methods that add complexity to a “thing”. This way we allow instances of a class to inherit behaviors defined with in our class. More on that later!

Things like “strings”, “integers”, “booleans”, “arrays”, and “hashes”are all examples of objects found in Ruby. However what we’re really interested is the Class object, which is considered to be a blueprint for other objects assigning it methods and attributes. A real world example would be if we looked at a Phone Class. A phone in itself is an object, but it has other less complex attributes like its camera, touch screen, and speaker. All are attributes and behaviors of our phone class and make up our devices!

If we were to create an overly simplified version of a phone class we would have

You’ve created your first phone class! Now that we have a class for phone we should probably create an instance of a phone. An instance is creating a new single object, in this case a single phone value for our phone class! The way we do that is by calling .new on our phone class. We can assign a variable to our so that we’re able to call that instance of the phone whenever we want. In this example I used iphone!

Now you’ve created your first instance of a class! A brand new iphone for your phone class. Lastly we should add some functionality to our phone by allowing it to pick up phone calls. Best way to do this is by adding a method inside our Phone class, something that would greet and incoming caller. Methods defined with in an objects class are instance methods, we call them that because we can now call this method on any instance we have created for our class.

Thanks to our instance method, our iphone instance can now answer a phone call. Congrats on building a fully functioning Phone class and an instance of a phone! Programming can get much more complicated from here, but understanding basic building block sets an important foundation for your future endeavors.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Learning rules versus gaining understanding

My Journey From Software Developer to Scrum Master will release V2.4.1 to support Anti-Reply

Database Trigger : Guards at a ticking point..

Micro Service Architecture — Design Patterns (Decomposition Pattern)

Making a club website during internship period

Hookstate the Simplest State Management Tool

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


More from Medium

Hunting down spooky JSON module redefinition via Oj

Convention over Configurations in Rails

The performance of .to_json in Rails sucks and there’s nothing you can do about it

Drop production database in Rails