New to Web Development?

New to web development? Get an explanation to common terms.

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, particularly ruby or rails, 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.

Classes

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

ruby
1class Person
2 has_many :friends
3end

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.

Objects

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

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:

ruby
1person_instance = Person.new

Method

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:

ruby
1class Dog
2 def bark
3 puts "Woof! Woof!"
4 end
5end

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

ruby
1small_dog = Dog.new
2small_dog.bark

The output would be:

Woof! Woof!

Function

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:

ruby
1def dog_barking
2 puts "Woof!"
3end

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

Conditional

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:

ruby
1puts "What happened?" if 2 == 1

Multiline:

ruby
1if 2==1
2 puts "What happened?"
3end

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:

ruby
1expression ? true_expression : false expression

In Ruby on Rails it would look like this:

rails
1<%= @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.

Inheritance

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

ruby
1class Cat
2 attr_accessor :name, :gender, :color
3end
4
5class Dog
6 attr_accessor :name, :gender, :color
7end
8
9class Bird
10 attr_accessor :name, :gender, :color
11end

Using inheritance this could be made DRYer:

ruby
1class Pet
2 attr_accessor :name, :gender, :color
3end
4
5class Cat < Pet
6end
7
8class Dog < Pet
9end
10
11class Bird < Pet
12end

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

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.

ruby
1class Person
2 def initialize(name)
3 set_name(name)
4 end
5
6 private
7
8 def private
9 # this is only available in this class
10 end
11end

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

Polymorphism

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.