It will cover all of the syntax and terminologies of ruby language which is must require to start with scripting in ruby and work with Metasploit exploit development and so on.
Note: This documentation is not for beginners at first you can learn from internet or books and then come here for only future revision.
Ruby is an open-source object-oriented scripting language invented in the mid-90s by Yukihiro Matsumoto. It is an interpreted, high-level, general-purpose programming language that supports multiple programming paradigms.
For Kali
sudo apt-get install ruby-full
For Windows Ruby Installer
puts "Hello Ruby!"
save file with .rb extension.
A Variable is a special container that stores data values.
puts ("Hello Elliot " + greet) # Hello Elliot Good Evening!
puts "Hello Elliot #{greet}" # Hello Elliot Good Evening!
There are four types of variables in Ruby:
- Local variables
name = "Elliot"
- Class variables
@@no_of_cert = 7
- Instance variables
@jobrole= "Pentester"
- Global variables
$global_var = "GLOBAL"
A data type tells the compiler or interpreter how the programmer intends to use the data.
- Numbers
- Strings
- Symbols
- Hashes
- Arrays
- Booleans
time = nil
means nothing in the time variable.
puts 5 / 9 # 0
puts 55 / 9 # 6
puts 2**3 # 8
puts 10 % 3 # 1
puts 4 % 10 # 4
A string is a group of letters that represent a sentence or a word. Strings are defined by enclosing a text within single (') or double (") quote.
Two strings can be concatenated using + sign in between them.
puts "Hello" + "Ruby"
Multiplying a number string with a number will repeat the string as many times.
=begin
Hello!
Hello!
Hello!
=end
"3" * 7 # 3333333
#!/usr/bin/ruby
data = {"Elliot" => "OSCP", "Alex" => "CEH", "Bob" => "CISSP"}
puts data["Elliot"]
puts data["Alex"]
puts data["Bob"]
Ruby arrays are ordered collections of objects. They can hold objects like integer, number, hash, string, symbol or any other array.
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
puts days[0] # Mon
puts days[10] # nothing will print here
puts days[-2] # Sat
puts days[2, 3]
# Wed
# Thu
# Fri
puts days[1..7]
=begin
Tue
Wed
Thu
Fri
Sat
Sun
=end
online_status = true
script_state = false
//code to be executed
end
//code if condition is true
else
//code if condition is false
end
if(condition1)
//code to be executed if condition1is true
elsif (condition2)
//code to be executed if condition2 is true
else (condition3)
//code to be executed if condition3 is true
end
#!/usr/bin/ruby
print "Enter your day: "
day = gets.chomp
case day
when "Tuesday"
puts 'Wear Red or Orange'
when "Wednesday"
puts 'Wear Green'
when "Thursday"
puts 'Wear Yellow'
when "Friday"
puts 'Wear White'
when "Saturday"
puts 'Wear Black'
else
puts "Wear Any color"
end
#This is single line comment.
This
is
multi line
comment
=end