Classes

Intro

Before learning classes, we need to learn how to run Ruby scripts outside of irb shell. This is very easy. We need to:

Let's try to create a simple file that would receive some arguments, show their count, and show them in a list. Try to write this program yourself first, and if it won't work than go back to our example. The program should be pretty easy, and its output should look like this:

And here is the implementation:
        p "Received #{ARGV.count} arguments#{if ARGV.empty? then '.' else ':' end}"
        ARGV.each_with_index { |arg, index| p ". " }
      

The first line of code may have one thing that we did not learn about yet: if/else/end operator. It should be pretty easy to understand. You could read it like this: if arguments are empty, show dot (as it would be the end of the sentence). If not empty, then show : since we are going to print the list of arguments. There is also a ternary operator that looks shorter then if/else/end but may be confusing for a beginner. It looks like: if_this_is_true ? do_this : if_not-do_this. If we used ternary operator, the first line would look like:

        p "Received #{ARGV.count} arguments#{ARGV.empty? ? '.' : ':'}"
      

If your code looks different but works the same way, it is absolutely fine, there are lots of ways to do the same thing in Ruby.

The second line iterates over all passed argument values with array's `each_with_index` method. If the array is empty, it will show nothing. If it has some values, it will print a string that has an index, and then argument's value.

Classes

Ruby is object-oriented which means we will use objects a lot. As you may already know, all objects belong to some Classes. String objects derive from String class, symbol objects from Symbol, arrays from Array, etc. The class is kind of a blueprint; it defines all important properties of its instances. Classes are not necessary data types - if your application has orders, it means you may have Order, Product, Customer classes.

Let's talk about people as an example. All people have similar basic properties, can do basic actions, etc. If our app worked with people, we would create a Person class, which will have a name, age, job, hobbies properties. Then we would use Person class to create objects of this class (instances):

        me = Person.new
        me.name = "Kyrylo"
        me.job = "Software Developer"
        me.hobbies = ["Programming", "Bycicling", "Kitesurfing"]
        my_friend = Person.new
        my_friend.name = "John"
      

Let's create our first class. Let's create an empty file first.

To create a class in Ruby, we need to use following syntax:

        class Person
          # some properties, methods, etc
        end
      

To continue learning classes, we need to come back to Variables first. There are four different variable types:

Let's also add empty Cat and Dog classes to our file so that it would look like this:

        class Person
        end

        class Dog
        end

        class Cat
        end
      

As you already know, to create object of each of these classes you would need to use:

        me = Person.new

        my_cat = Cat.new

        my_dog = Dog.new
      

Empty classes are not that interesting. Let's check next chapter to make them do something.

Book Index | Next