DEV Community

Dane Dawson
Dane Dawson

Posted on

Ruby Newby, Classes introduction Part 1

The object Class is a useful and expansive tool in Ruby programming, but because of the multitude of applications and uses it has in more advanced programming techniques it can be difficult to find a basic and fluid introduction to what they are and how to use them. This will be my attempt at a (hopefully) concise and useful introduction to Classes and their components.

First let's define what a Class is. In broad terms Class is used as an Object one can use to create objects with defined attributes and methods to interact with those objects. For this article we will think of a Class as a miniature MakerBot full of very basic (and tiny) worker robots. The MakerBot Class can be used to create MakerBot Objects and within the MakerBot class is a place to keep the blueprint for what it makes (these are called attributes and will be covered in more detail in future posts). Unfortunately the workers don't know how to do anything you don't tell them how to do exactly, so there is also a place full of instruction manuals for the workers to follow (these will be the methods we write within the Class itself). Sounds simple enough, right? Let's break it down!

First, we build our MakerBot (the class):

class MakerBot
end

That's it! We now have a Class called MakerBot. We have to notate that we are making a class, and then name our class. With Ruby you write Class name in CamelCase, combining all words into one line and capitalizing each letter. If our MakerBot was just a factory, for instance, we may name our class Factory. The only downside to our MakerBot as is...it doesn't do anything! Let's give this Class something that happens when you call on it.

class MakerBot
    def initialize
        print "It works!"
    end
end

Initialize is part of an inhereted method for the Class. Inhereted methods are methods that come with an object inherently, without you having to type any code. An example would be array.length, where length is the inherited method that arrays carry, allowing programmers to discover the length of an array simply by calling length against it. Using initialize and typing (ClassName).new creates an Object (in our case, a MakerBot). The object it makes is defined by whats in the initialize method within the Class, and is where we will be adding in attributes and some specific methods in the future. Right now let's try calling our Class to create a new MakerBot Object.
We can now call our MakerBot by typing:

MakerBot.new

and it should return

It works!

Hey look! Our code did something! But having some words on a screen is not really having our own MakerBot...Let me add some lines of code to give it some attributes and really give our MakerBot some substance! Well...digital substance.

class MakerBot
    def initialize(name, product, owner)
    end
end

You can see we added arguments to the initialize method, this will allow us to input values to give our MakerBot some details to make it more like an actual object! Let's try running it now and see what happens...

MakerBot.new("Steve", "Waffle-fries", "The Master Programmer")

Huh...that's strange...Nothing happened? The good news is, you didn't type anything wrong! The problem here is that although we are making a new MakerBot and giving it all these cool attributes, we aren't actually saving anything...anywhere...ever! On the next post I will continue exploring attributes, and how to save data points in temporary memory. As a teaser, by the end of next lesson the following code will make sense!

class MakerBot
    attr_accesor: :name, :product
    attr_reader: :owner

    @@all = []

    def initialize(name, product, owner)
        @name = name
        @product = product
        @owner = owner
        @@all << self
    end
end

Until next time, happy coding!

Oldest comments (0)