Class[y] Cows
August 15, 2014
Object-Oriented Programming
Ruby is an object-oriented programming (OOP) language - it manipulates programming constructs called objects. Objects have methods (a set of functions) and variables (a set of attributes).
In this post, we want to describe some cows on our farm, so each cow will be an object. Since all cows have common attributes and actions, we can define all of those with something called a class.
Classes
A class is a way of organizing and producing objects with similar methods and variables. Classes are used to construct an object in a DRY manner. The syntax to create a new class is pretty simple:

Class Variables
There are several types of variables available for use within a Ruby class:
Local Variables: Defined in a method and not available outside that method. Local variables begin with a lowercase letter or underscore. For our cows, a _milk_remaining variable could be used within a milk method, since we only want the milk method to change that variable. After we're done milking, this variable is discarded and the milk gets refilled.
Instance Variables: Available across all methods in a class for any object, but can change from object to object. Instance variables begin with @. Let's make @name an instance variable; after all, every cow should know its name.
Class Variables: Members of a certain class, but shared by all objects. Class variables begin with @@. If we make @@brand_initials a class variable, it will be accessible by all of our cows. We wouldn't want to lose them, would we?
Global Variables: Available everywhere, which means it can be changed from anywhere in the program. This is generally not a good idea. Global variables begin with $. $farm_address could be a global variable but, like I said, giving your cows the ability to change your address is probably not a good idea!
Initialize Method
A blank object is not very exciting. In order to start using your object, it must first be initialized (assuming it has any instance variables that needs initializing). This is done via the initialize method. Ruby will pass any arguments you pass to ClassName.new on to initialize on the new object. You can then use normal variable assignments and methods to initialize the state of the object. In this example, a Cow class is presented. Its initialize method will take a name and breed argument, and assign them to instance variables.

Public vs. Private Methods
A method in Ruby is a set of expressions that returns a value. With methods, one can organize their code into subroutines that can be easily invoked from other areas of their program. A method can be defined as a part of a class or separately. A method can be public or private. Public methods allow for an interface with the rest of the program; they can be called outside the class. Public methods are the default. On the other hand, private methods are not accessible for outside objects. To make a method private, just put 'private' above the method(s) you want to be private. However, in order to access the private methods, public methods within the class must access the information.
Let's Code
Now that we know the parts that make up a class, let's build one:

Resources
Ruby for Newbies: Working with Classes
Ruby Class Tutorial
If you want practice: CodeAcademy