DEV Community

Cover image for Ruby Classes
Sean
Sean

Posted on

Ruby Classes

Ruby's Got Class!😁

CLASS
So, the topic of the day is class.

You mean Arrays, Hashes, and stuff like them right?

Not exactly, but you're on the right track. Those are builtin classes and in Ruby we have OOP, but in the case of Ruby I like to call it POOP.

You lost me.

OOP stands for Object Oriented Programming.

Then what's POOP?

If you didn't already know, Ruby is an object oriented programming language. This means that everything is an object(except for functions and methods). So, POOP is my way of differentiating OOP from regular Ruby. POOP stands for, Personalized Objects made by you Oriented/tailored for your Program.

That's kind of long.

Yeah, but it helped me.

The Basics of OOP POOP 💩

Learning OOP is a great skill that's pretty much universal. Most popular languages have OOP. Python, Ruby, JavaScript, C++, and Java all use OOP.

Okay, so where do I start?

Let's start with making our first class.

Basic class syntax

Here's what a very basic Ruby class looks like:

class Somename
  initialize(para1, para2, para3, etc)
    @para1 = para1
    @para2 = para2
    @para3 = para3
    @etc = etc
  end
end
Enter fullscreen mode Exit fullscreen mode

Okay, this might be a lot to take in but it will be explained. Let's start small; recap: a method is a function bound to a class/object. The initialize method in Somename is usually the first thing in a class. It contains all the values that will be set to the class. the @ sign before each redefined variable is necessary, it means that it's an instance variable.

What's that?

It's a variable that can be accessed inside of the class, and through an instance of it. class variables have two @ signs at the front of the variable and are like instance variables except that they cannot be accessed by instances of the class. Here's an example of the difference:

class Person
  def instance_var
    @instance = "instance variable."
  end
  def self.class_var
    @@class = "class variable."
  end
  def sayhi
    puts "Hi I'm a class method containing an #{self.instance_var}"
  end
  def sayhi
    puts "Hi I'm a class method containing a #{self.class_var}"
  end
end
per = Person.new
per.sayhi #=> Hi I'm a class method containing an instance variable.
Person.sayhi #=> Hi I'm a class method containing a class variable.
Enter fullscreen mode Exit fullscreen mode

Wait, I don't see the initialize method, why is that?

The initialize method is only needed for classes that need input to work. So if the class needs no input then you likely won't see initialize in the code.

Okay, but why did the same method give a different output?

Because they're not the same method; the reason why they're not is very simple. The sayhi method is defined, then redefined. We don't get an error from this, because one only uses a class variable and the other uses an instance variable. As mentioned before, instance variables can be accessed by instances of a class, and inside the class, but not by the class itself.Class variables cannot be accessed by instances of the class,and only by the class itself. This means that Ruby identifies the separate use of either class or instance variable, pointing to the respective sayhi method.

Hold up, English please!

Basically, for the class itself Ruby will point to the method using class variables, but for instances of the same class Ruby will point to the method using instance variables.

Okay, I understand class and instance variables a bit, but what's an instance of a class any ways.

An instance of a class is a version of it. So, kind of how there's the Array class, but you can have more than one Array in your program. The different Arrays will have different lengths, values stored, etc., but they all are still Arrays.

In simple terms...?

An apple is a type of fruit. There are different types of apples, all of them have something that makes them unique, but at their core they still have seeds, they still grow from a tree, and they still have a elliptical shape. So, to be clear, instances of a class are just like apples.

Weird analogy, but OKAY!

A Few Practical Uses of A Class 😎

The only places you will be forced to use a class is game-making. I say this with experience since I've made more than one project that happened to be a game. In most communities using classes aren't standard, therefore, not seen in well made code. Many YouTubers use classes when doing a game making tutorial of any kind. In most languages they have alternatives to OOP, like JS with objects(they're not the same btw). Most programmers don't support the use of classes in code, since it often is very difficult to read. You might think:

It didn't seem so bad to me. In fact I thought I should start using it?

But here's some code in Ruby using classes:

class player
    def initialize(self, x, y, wid, len)
        @x = x
        @y = y
        @width = wid
        @length = len
        @speed = 25
        @c_left = False
        @c_right = False
        @jumping = False
        @is_standing = True
        @jumpCount = 10
        @walk_count = 0
        @difference = @length - @speed
        @hitbox = (@x - 5, @y, 70, 70)
        @health = 100
        @continueing = True
   end
end

Enter fullscreen mode Exit fullscreen mode

This code is super messy. Sadly that's the problem with classes, when they're used for big operations(like creating a character) things get confusing. All of that code was just in the initialize method.

So I Guess It Is POOP(No I Lied 🤥)

Now, now. Let's not poop on POOP 💩. There are great uses for it. If it was so bad most languages wouldn't have it, and eventually it would become obsolete. Making your own class is like making a module(if you don't know what that is that's fine), for it to be useful you have to make it useful.

That's It For Now! 🤗

BYE!!!
Hope you learned something!

Top comments (0)