Ruby Cheat Sheet

In Ruby

Ruby is a dynamic, open-source programming language that has gained immense popularity in recent years. It was created in the mid-1990s by Yukihiro Matsumoto, a Japanese programmer who wanted to create a language that was both easy to use and powerful. Ruby is known for its simplicity, flexibility, and readability, making it a popular choice for web development, scripting, and automation.

One of the key features of Ruby is its object-oriented programming (OOP) model. Everything in Ruby is an object, which means that developers can easily create and manipulate complex data structures. Ruby also has a strong focus on code readability, with a syntax that is easy to understand and follow. This makes it an ideal language for beginners who are just starting to learn programming.

Another advantage of Ruby is its extensive library of pre-built modules, known as gems. These gems can be easily installed and used in Ruby projects, saving developers time and effort. Ruby also has a large and active community of developers who contribute to the language and its ecosystem, ensuring that it remains up-to-date and relevant.

Ruby is widely used in web development, with popular frameworks such as Ruby on Rails and Sinatra. These frameworks provide developers with a set of tools and conventions for building web applications quickly and efficiently. Ruby is also used in automation and scripting, with tools such as Chef and Puppet.

This cheat sheet provides an overview of the most commonly used Ruby syntax and features. For more detailed information, please refer to the official Ruby documentation.

Variables

SyntaxDescription
x = 5Assigns the value 5 to the variable x
y = ""hello""Assigns the string ""hello"" to the variable y
z = [1, 2, 3]Assigns an array [1, 2, 3] to the variable z
a = {name: ""John"", age: 30}Assigns a hash {name: ""John"", age: 30} to the variable a

Data Types

TypeDescription
IntegerWhole numbers
FloatDecimal numbers
StringText
Booleantrue or false
ArrayOrdered list of elements
HashCollection of key-value pairs

Operators

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulo (remainder)
**Exponentiation
==Equality
!=Inequality
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
&&Logical AND
||Logical OR
!Logical NOT

Control Structures

Conditional Statements

SyntaxDescription
if condition
# code to execute
end
Executes code if condition is true
if condition
# code to execute
else
# code to execute
end
Executes first block of code if condition is true, otherwise executes second block
if condition1
# code to execute
elsif condition2
# code to execute
else
# code to execute
end
Executes first block of code if condition1 is true, second block if condition2 is true, otherwise executes third block

Loops

SyntaxDescription
while condition
# code to execute
end
Executes code repeatedly while condition is true
until condition
# code to execute
end
Executes code repeatedly until condition is true
for element in collection
# code to execute
end
Executes code for each element in collection
loop do
# code to execute
break if condition
end
Executes code repeatedly until break is called or condition is true

Methods

SyntaxDescription
def method_name(arg1, arg2)
# code to execute
end
Defines a method with arguments
method_name(arg1, arg2)Calls a method with arguments
def method_name(arg1=default_value)
# code to execute
end
Defines a method with default argument values
def method_name(*args)
# code to execute
end
Defines a method with variable-length argument list
def method_name(&block)
# code to execute
end
Defines a method that takes a block

Classes

SyntaxDescription
class MyClass
# code to execute
end
Defines a class
my_object = MyClass.newCreates an instance of a class
class MyClass
attr_accessor :my_attribute
end
Defines a class with an attribute accessor
class MySubclass < MyClass
# code to execute
end
Defines a subclass of a class

Modules

SyntaxDescription
module MyModule
# code to execute
end
Defines a module
include MyModuleIncludes a module in a class or module
extend MyModuleExtends a class or module with a module

Exceptions

SyntaxDescription
begin
# code to execute
rescue
# code to execute
end
Executes code and catches any exceptions
raise ""Error message""Raises an exception with an error message
raise ExceptionType, ""Error message""Raises a specific type of exception with an error message

References