Ruby – Creating classes and objects

To create a new class, you use the class keyword:

class Employee
   # code goes here. 
end

Previously we mentioned that variable names always start with a lower case. However in the case of classes, the convention is that class names always starts with an upper case, and it follows the CamelCase style. The convention also states that accronyms that are in uppercase too. Here’s an example:

class XMLReaderAndWriter
  # code goes here. 
end 

In ruby you have a special keyword called “new” which is used to create an object from a class (i.e. instantiate a class), e.g.:

NewObject = ClassName.new

For, example:

NewObject = Employee.new

The “new” keyword will automatically trigger the class’s “initiliaze” method. The initialize method is the equivalent to c#’s constructor method.