Ruby Cheat Sheet

Posted by Candice Parker on February 29, 2020

Defining a Class in Ruby

To implement object-oriented programming by using Ruby, you need to first learn how to create objects and classes in Ruby.

A class in Ruby always starts with the keyword class followed by the name of the class. The name of the class should always start with a capital letter:

class Customer end

You terminate a class by using the keyword end. All the data members in the class are between the class definition and the end keyword.

Variables in a Ruby Class

Ruby provides four types of variables:

Local Variables − Local variables are the variables that are defined in a method. Local variables are not available outside the method. You will see more details about method in subsequent chapter. Local variables begin with a lowercase letter or _.

Instance Variables − Instance variables are available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by the at sign (@) followed by the variable name.

Class Variables − Class variables are available across different objects. A class variable belongs to the class and is a characteristic of a class. They are preceded by the sign @@ and are followed by the variable name.

Global Variables − Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. The global variables are always preceded by the dollar sign ($).

Creating Objects in Ruby using new Method Objects are instances of the class. You will now learn how to create objects of a class in Ruby. You can create objects in Ruby by using the method new of the class.

The method new is a unique type of method, which is predefined in the Ruby library. The new method belongs to the class methods.

Here is the example to create two objects cust1 and cust2 of the class Customer −

cust1 = Customer. new cust2 = Customer. new

Here, cust1 and cust2 are the names of two objects. You write the object name followed by the equal to sign (=) after which the class name will follow. Then, the dot operator and the keyword new will follow.

Ruby Instance Variables

Instance variables begin with @. Uninitialized instance variables have the value nil and produce warnings with the -w option.

Here is an example showing the usage of Instance Variables.

class Customer def initialize(id, name, addr) @cust_id = id @cust_name = name @cust_addr = addr end def display_details() puts “Customer id #@cust_id” puts “Customer name #@cust_name” puts “Customer address #@cust_addr” end end

// Create Objects cust1 = Customer.new(“1”, “John”, “Wisdom Apartments, Ludhiya”) cust2 = Customer.new(“2”, “Poul”, “New Empire road, Khandala”)

// Call Methods cust1.display_details() cust2.display_details()

//Methods executed Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Customer id 2 Customer name Poul Customer address New Empire road, Khandala

Here, @cust_id, @cust_name and @cust_addr are instance variables. This will produce the following result −

Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Customer id 2 Customer name Poul Customer address New Empire road, Khandala

Ruby Class Variables

Class variables begin with @@ and must be initialized before they can be used in method definitions. Referencing an uninitialized class variable produces an error. Class variables are shared among descendants of the class or module in which the class variables are defined.

Overriding class variables produce warnings with the -w option.

Here is an example showing the usage of class variable:

class Customer @@no_of_customers = 0 def initialize(id, name, addr) @cust_id = id @cust_name = name @cust_addr = addr end def display_details() puts “Customer id #@cust_id” puts “Customer name #@cust_name” puts “Customer address #@cust_addr” end def total_no_of_customers() @@no_of_customers += 1 puts “Total number of customers: #@@no_of_customers” end end

// Create Objects cust1 = Customer.new(“1”, “John”, “Wisdom Apartments, Ludhiya”) cust2 = Customer.new(“2”, “Poul”, “New Empire road, Khandala”)

// Call Methods cust1.total_no_of_customers() cust2.total_no_of_customers()

//execute $ruby main.rb Total number of customers: 1 Total number of customers: 2

Ruby Local Variables

Local variables begin with a lowercase letter or _. The scope of a local variable ranges from class, module, def, or do to the corresponding end or from a block’s opening brace to its close brace {}.

When an uninitialized local variable is referenced, it is interpreted as a call to a method that has no arguments. Assignment to uninitialized local variables also serves as variable declaration. The variables start to exist until the end of the current scope is reached. The lifetime of local variables is determined when Ruby parses the program.

In the above example, local variables are id, name and addr.

.Ruby Constants

Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally.

Constants may not be defined within methods. Referencing an uninitialized constant produces an error. Making an assignment to a constant that is already initialized produces a warning.

class Example VAR1 = 100 VAR2 = 200 def show puts “Value of first Constant is #{VAR1}” puts “Value of second Constant is #{VAR2}” end end

// Create Objects object = Example.new() object.show

//execute $ruby main.rb Value of first Constant is 100 Value of second Constant is 200