New to Web Development?
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.
Simply, a Ruby class is the definition (or classification) of a single type of object. For example:
ruby1class Person2 has_many :friends3end
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:
ruby1person_instance = Person.new
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:
ruby1class Dog2 def bark3 puts "Woof! Woof!"4 end5end
Now any dogs you create can now bark by calling the bark method:
ruby1small_dog = Dog.new2small_dog.bark
The output would be:
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:
ruby1def dog_barking2 puts "Woof!"3end
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
ruby1puts "What happened?" if 2 == 1
ruby1if 2==12 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
case and many others. A very common and concise way to do a simple if statement is:
ruby1expression ? true_expression : false expression
In Ruby on Rails it would look like this:
rails1<%= @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 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 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:
ruby1class Cat2 attr_accessor :name, :gender, :color3end45class Dog6 attr_accessor :name, :gender, :color7end89class Bird10 attr_accessor :name, :gender, :color11end
Using inheritance this could be made DRYer:
ruby1class Pet2 attr_accessor :name, :gender, :color3end45class Cat < Pet6end78class Dog < Pet9end1011class Bird < Pet12end
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.
ruby1class Person2 def initialize(name)3 set_name(name)4 end56 private78 def private9 # this is only available in this class10 end11end
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.