Ruby minimal

New to Web Development?

July 04, 2015|Ruby

When I was first learning ruby and rails I would hear terms used and I wasn't exactly sure what it was. I could understand it based on the context but if I had to explain it to someone I wouldn't know how to do that.

So, if you are new to web development here are some terms that you will likely hear and need to understand.

Classes and Objects

A class defines a concept (such as a Person), and an object is a single thing based on a class (such as a "Chris" or a "Mrs. Smith").

A class is the classifiction, whereas an object is the actual object or "thing" itself.


Simply, a Ruby class is the definition (or classification) of a single type of object. For example:

class Person
    has_many :friends

Ruby classes always start with a capital letter. Some examples are User, Person, Place, Topic, Post, and so forth.

Classes can inherit features from other classes while still having unique features of their own.


Ruby is an object-oriented programming language and pretty much everything in Ruby is an object. An object is simply a single instance of a class.

You can take concepts in the real world and define and operate upon them in Ruby. For instance, "people", "books", "posts", "comments" and so on. Ruby can take these concepts and act upon them, create relationships between them, and so on. In order to do this you must create an object instance.

For example, if you have a class Book the object would be a single specific book, like "the Alchemist". As another example, an object of a class Person is a single person.


Instantiation is creating a new object instance (like "The Alchemist" or a single person using the examples above). In Ruby you use the Class method new to instantiate a new object. For example:

person_instance =


Methods are like instructions for your object to perform. They're very important because they are what makes objects do things.

For instance, if we had a Dog object and we wanted it to have a bark method, you would create it like this in Ruby:

class Dog
    def bark
        puts "Woof! Woof!"

Now any dogs you create can now bark by calling the bark method:

small_dog =

The output would be:

Woof! Woof!


Functions are very similar to methods but tend to be more general and not tied to any particular class or object. They are called like you call a method. It is essentially an action that can take place upon an object. Function and method are used interchangeably in Ruby and typically called methods and not functions.

Here's an example:

def dog_barking
    puts "Woof!"

Now to call this method you simply need to call: dog_barking and the output will be: Woof!


Conditional logic is a form of flow control for your code to follow. The simplest form of conditional logic is if or unless.

Single Line:

puts "What happened?" if 2 == 1


if 2==1
    puts "What happened?"

This is two ways of doing the same thing. If this conditional logic resolves to be false the string "What happened?" will not be printed to the screen. In fact, it would be as if this wasn't even in your code at all.

You can also use else, elseif, unless, case and many others. A very common and concise way to do a simple if statement is:

expression ? true_expression : false expression

In Ruby on Rails it would look like this:

<%= @user.present? ? "Hello!" : "Sign Up" %>

This is an if expression asking if the user is present. If so it will print "Hello!" and if it results in false it will print "Sign Up".

Class Variables and Instance Variables

Class and instance variables are very similar. They have two main ways they differ. First, is their scope. Class variables have a wider scope and instance variables are specific just to an instance of an object. Second, is the way they're called. Instance variables are called with one @ symbol and class variables are called with two @ symbols (like so @@).

Instance Variables

Instance variables are specific to an instance of an object (hence instance variables) and are accessible only within that instance. They are accessed using the @ operator. Outside of the class definition, you can only use that instance variables public methods.

Class Variables

Class variables have scope within the current class (as opposed to within specific objects of that class as the instance variable). Class variables are particularly useful for storing information relevant to all objects of a certain class. They are accessed using the @@ operator.


Classes can inherit features from other classes while still having unique features of their own. Example:

class Cat
    attr_accessor :name, :gender, :color

class Dog
    attr_accessor :name, :gender, :color

class Bird
    attr_accessor :name, :gender, :color

Using inheritance this could be made DRYer:

class Pet 
    attr_accessor :name, :gender, :color

class Cat < Pet

class Dog < Pet

class Bird < Pet

In both of these examples, Bird, Dog, and Cat have :name, :gender, and :color as "attr_accessor". You can see how the second example is better by using inheritance.


Encapsulation is the ability for an object to have certain methods and attributes available for use publicly (from any section of code), but for others to be visible only within the class itself or other objects of the same class.

class Person
    def initialize(name)


    def private
        # this is only available in this class

This keeps your application clean and agile so that you can change it without worrying that it's going to break in other places.


This is a concept of writing code that can work with objects of multiple types and classes at once. An example is the + method. It works for adding numbers, joining strings, and adding arrays together.

There's also a polymorphic relationship. If you have an app that has a Post, Forum, Tutorial, and Comment classes. You want each of them to have a relationship with the comment class. Polymorphic relationships are a way to extend the same relationship across classes. So, that the Post, Forum, and Tutorial all can be commented on.

Learn to Effortlessly Create Anything in CSS

Get access to my free courses, best articles, and tips once a week.

I won't send you spam, promise. If it ever feels that way you can unsubscribe at any time.

Sign Up For Free


Sign In