> For the complete documentation index, see [llms.txt](https://www.cs4066.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.cs4066.com/unit-6/ruby-tutorial.md).

# Ruby Tutorial

## **1: Setting up Ruby**

Before we dive into learning Ruby, make sure you have the latest version of Ruby installed on your system. You can download it from the official Ruby website.

Once you have Ruby installed, open a terminal or command prompt and type `ruby -v` to verify that it's working correctly.

## **2: Basic Syntax**

Ruby has a simple syntax that is easy to learn. Here are some basic concepts:

* **Variables**: In Ruby, variables start with a dollar sign (`$`). For example: `$my_variable = "Hello World!"`
* **Strings**: Strings are enclosed in quotes or double quotes. For example: `"Hello World!"` or `'Hello World!'`
* **Numbers**: Numbers can be integers or floating-point numbers. For example: `10`, `3.14`
* **Operators**: Ruby has the usual arithmetic operators (`+`, `-`, `*`, `/`) as well as comparison operators (`==`, `!=`, `<`, `>`)
* **Methods**: Methods are blocks of code that can be called multiple times with different inputs. For example: `def greet(name); puts "Hello, #{name}!"; end`

## **3: Control Structures**

Control structures determine the flow of your program's execution.

* **If-else statements**: Used to make decisions based on conditions.

```ruby
x = 10
if x > 5
  puts "x is greater than 5"
else
  puts "x is less than or equal to 5"
end
```

* **Loops**: Used to repeat a block of code for a specified number of times.

```ruby
i = 0
while i < 5 do
  puts "Loop iteration: #{i}"
  i += 1
end
```

* **Case statements**: Used to compare an expression with multiple values and execute different blocks of code accordingly.

```ruby
x = "apple"
case x
when "banana"; puts "You chose banana!"
when "apple"; puts "You chose apple!"
else; puts "Invalid choice!"
end
```

## **4: Arrays and Hashes**

Arrays and hashes are two fundamental data structures in Ruby.

* **Arrays**: Ordered collections of elements, indexed by integers starting from 0.

```ruby
fruits = ["apple", "banana", "orange"]
puts fruits[1] # prints "banana"
```

* **Hashes**: Unordered collections of key-value pairs.

```ruby
person = {name: "John Doe", age: 30}
puts person[:age] # prints 30
```

## **5: Methods and Blocks**

Methods are reusable blocks of code that take arguments and return values. Blocks are similar to methods but can be defined inline.

* **Method definition**: Define a method using the `def` keyword.

```ruby
def greet(name)
  puts "Hello, #{name}!"
end

greet("Alice") # prints "Hello, Alice!"
```

* **Block definition**: Define a block using the `do`...`end` or `{}` syntax.

```ruby
[1, 2, 3].each do |x|
  puts x
end

# equivalent to:
[1, 2, 3].each { |x| puts x }
```

## **6: Object-Oriented Programming (OOP)**

Ruby is an object-oriented language that supports encapsulation, inheritance, and polymorphism.

* **Class definition**: Define a class using the `class` keyword.

```ruby
class Car
  def initialize(make, model)
    @make = make
    @model = model
  end

  def honk
    puts "Beep!"
  end
end

my_car = Car.new("Toyota", "Corolla")
my_car.honk # prints "Beep!"
```

* **Inheritance**: Create a new class that inherits from an existing class.

```ruby
class ElectricCar < Car
  def initialize(make, model)
    super(make, model)
    @battery_capacity = 100
  end

  def charge
    puts "Charging battery..."
  end
end

my_electric_car = ElectricCar.new("Tesla", "Model S")
my_electric_car.honk # prints "Beep!"
my_electric_car.charge # prints "Charging battery..."
```

## **7. Ruby Standard Library**

Ruby comes with a comprehensive standard library that includes modules like `IO`, `File`, and `Socket`.

* **File I/O**: Read and write files using the `File` class.

```ruby
file = File.open("example.txt", "w")
file.puts "Hello World!"
file.close

# or use the block syntax:
File.open("example.txt", "w") do |file|
  file.puts "Hello World!"
end
```

* **Networking**: Use the `Socket` class to create network connections.

```ruby
require 'socket'

sock = TCPSocket.new('www.example.com', 80)
sock.puts "GET / HTTP/1.0\r\nHost: www.example.com\r\n\r\n"
puts sock.read

# or use the block syntax:
TCPSocket.open('www.example.com', 80) do |sock|
  sock.puts "GET / HTTP/1.0\r\nHost: www.example.com\r\n\r\n"
  puts sock.read
end
```

## **8: Ruby Gems and Bundler**

Ruby has a vast ecosystem of third-party libraries and frameworks, known as gems.

* **Gems**: Install gems using the `gem install` command.

```bash
gem install rails
```

* **Bundler**: Use Bundler to manage gem dependencies in your project.

```ruby
# Gemfile:
source 'https://rubygems.org'
gem 'rails', '~> 6.0'

# run:
bundle install
```

## **9: Ruby Metasploit Integration**

Now that you have a solid foundation in Ruby, let's explore how to integrate it with Metasploit!

* **Metasploit API**: Use the Metasploit API to interact with the framework programmatically.

```ruby
require 'msf/core'

# create an instance of the Msf::Core class:
core = Msf::Core.new

# load a module:
module = core.modules.load('exploit/multi/handler')

# run the exploit:
core.exploits.run(module)
```

* **Metasploit Console**: Use the Metasploit console to interact with the framework interactively.

```bash
msfconsole
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.cs4066.com/unit-6/ruby-tutorial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
