Eunice Choi
Web Development Student | Dev BootCamp SF

Ruby Classes


September 28, 2015

Do you remember how Ruby is an object-oriented language? The ability to create classes and objects allows Ruby to be very powerful. Classes are the basic "templates" from which objects are created. Objects are pretty much all the variables, functions, and data collection structures that we know. A great example would be a hash. Creating a new hash is actually creating a new instance of the class Hash. In fact, classes are objects, each an instance of the class Class! Classes are useful because they allow you to create variables and methods for the resulting object of each instance.


Let's create a class together. Say that you want to build a class that returns some valuable information about each song in your party playlist. Remember that class names start with an uppercase letter:

                        class Song
                          def initialize(name, artist, genre)
                            @name     = name
                            @artist   = artist
                            @genre    = genre
                          end
                        end

I went ahead and inserted the first method for our class: def initialize. Initialize is a special Ruby method that allows you to pass in parameters when you call a new instance of the class. To create a new Song object you may type this in your irb:

>>first_song = Song.new("Hotline Bling", "Drake", "Hip Hop & Rap")

You can also see that I set each parameter to a variable with an @ sign in front of it. These are called instance variables. Whenever I create a new instance of the class Song, I am creating a new object that represents a different song. Therefore, we need each of our Song objects to hold its own song title, artist, and genre. To do this, we need to store these as instance variables. Think of it like each song having its own ID. Instance variables also work across methods within the same class. Now, let's add some more code:

                        class Song
                          def initialize(name, artist, genre)
                            @name     = name
                            @artist   = artist
                            @genre    = genre
                          end

                          def name
                            @name
                          end

                          def artist
                            @artist
                          end

                          def genre
                            @genre
                          end
                        end

Here are three new methods for our class Song. Each method returns one of the three parameters. Simple, right? Now, see this:

                        class Song
                          attr_reader :name
                          attr_reader :artist
                          attr_reader :genre

                          def initialize(name, artist, genre)
                            @name     = name
                            @artist   = artist
                            @genre    = genre
                          end
                        end

Believe it or not, this code does the exact same thing as the one before. Attribute readers allow you to "get" a variable from your class. So instead of tediously writing blocks of code that solely have the function of returning a variable, you can simply set that variable as an attr_reader and get it directly! To return the artist of your first song, you would type:

>> first_song.artist

There are also attribute writers, which allow you to "set" or change a variable. In this case, we won't use them since it is very unlikely that a song name, artist, or genre would change. Attribute accessors can do the job of attribute writers AND attribute readers. They should be used when only necessary.



I hope you've learned a thing or two about Ruby classes. Classes are powerful tools in object oriented languages and are used for practically creating everything that you use in Ruby.