DEV Community

Charlie E. Paterson
Charlie E. Paterson

Posted on

Getting into programming with classes

There's a certain threshold that one must eventually cross when getting to grips with any language (although I currently can only speak from a basic grasp on ruby). You might be thinking, iterating over hashes perhaps? Or is this wrapping our heads around control flow and how to get data to travel properly between methods? Or perhaps even committing to memory - and coming to grips with the workings behind - methods with hashes?

Well, no, not quite. But hashes, arrays, strings and so forth respond to the methods that they do so because these methods are part of their class.
The array class has it's own methods, the string class has it's own methods...and can even have a range of array methods called upon it, but that's beside the point (even if all too useful!).
For you see, reader, one can make their very own classes, as follows:

class SomeClass
def initialize(argument, other_argument)
@argument = argument
@other_argument = other_argument
end
end

One might be lead to think there's little much else to making their very own methods but this, too, has some exciting quirks that it's important to get a handle on if one wishes to properly harness the potential of custom classes!

As you may have noticed, my example contains an initialize method - this is standard practice if one wishes to individualise the data held by instances (objects) belonging to the class (such as name, age, temperature, you name it!), and will run whenever you do
"some_object = SomeClass.new"!

The '@' symbol I've placed before the variable names in the body means that their assignment in the method will apply across the entire instance, rather than just that one method (remember that the only data methods will usually provide outside of themselves is that which is 'return'ed).

Of course we could set about coding methods to return each of these instance attributes we have set, but a much quicker way to be able to return them is as follows:

class OtherClass
attr_reader :argument, :other_argument
(initialize variable and so on)
end

With attr_reader, you're all set to return the value of any of the chosen instance attributes without having to code methods to return each and every one of them. However, if you want to be able to change these values you need attr_writer, and to do both you'll need attr_accessor.

From here you can write methods to make your classes do all kinds of things: maybe one of these instance variables is an array, and you want a method to push instances of some other class into it? Or maybe you want said instance to have a method to push itself when called upon it? Maybe a class called 'Villain' or 'Student' could hold a whole slew of information in the form of class attributes, allowing a whole different approach to the student directory project of yesterweek?

While I do have to admit that custom classes do still feel ever-so-slightly ominous in their novelty, I'm looking forward to really playing with the full roster of their capabilities in my journey through the world of code.

Top comments (0)