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.
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.
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.
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.
fruits = ["apple", "banana", "orange"]
puts fruits[1] # prints "banana"
Hashes: Unordered collections of key-value pairs.
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.
def greet(name)
puts "Hello, #{name}!"
end
greet("Alice") # prints "Hello, Alice!"
Block definition: Define a block using the
do
...end
or{}
syntax.
[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.
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.
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.
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.
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.
gem install rails
Bundler: Use Bundler to manage gem dependencies in your project.
# 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.
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.
msfconsole
Last updated