On the official website, Rubi is described as “A dynamic, open-source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write”, which is true.
Ruby has been losing popularity to Python for pretty much the same characteristics but still very much adopted by the Linux and Hacking community (many exploits were written in Ruby).
Working with strings:
varName = "!" intName = 5 print ("String" + varName) puts ("String" + intName.to_s)
Booleans and Numbers:
booleanName = true intName = -99 intName += 9 puts intName.abs()
Float (decimals):
floatName = 0.00001 puts floatName.round()
Absence of a value:
notAssignedValue = nil
Scaping characters, changing cases, getting information, and indexes:
varName = "Test print \"." puts varName.upcase() varName = "New line.\n" puts varName.downcase() varName = " Extra spaces. " puts varName.strip() puts varName.length() puts varName.include? "Extra" puts varName.index("E") puts varName[5] puts varName[5,5]
Math operations:
puts Math.sqrt(64) puts Math.log(10)
Prompting for information and converting it to numbers:
strName = gets() strName = gets.chomp() puts strName.to_i puts strName.to_f
Working with arrays:
arrayName = ["A","B","C"] puts arrayName arrayName = Array.new arrayName[0] = "a" puts arrayName.length() puts arrayName.include? "a" puts arrayName.sort() puts arrayName.reverse()
Dictionary / Hash:
hashName = { "A" => "X", "B" => "Y", "C" => "Z" } puts hashName["B"]
Creating and calling functions and IF statement cases:
def methodName(message, print=false) if print and message puts message elsif !message puts "no message" else puts "not set to print" end return "first", "second" end puts methodName("Hi!") puts methodName("Hi!",1) puts methodName(false,1)
While looping:
while true puts "Executed!" break end
Looping through a list:
listName = ["M","N","O"] for line in listName puts line end listName.each do |line| puts line end
For looping:
for i in 1..3 puts i end 3.times do |i| puts i end
Handling errors:
begin x = 1 / 0 rescue ZeroDivisionError puts "Impossible to devide by Zero" rescue TypeError => e puts e end
Classes and objects:
class ClassName attr_accessor :attr1, :attr2, :attr3 end objName = ClassName.new() objName.attr1 = "Text" objName.attr2 = 10 objName.attr3 = 0.5 puts (objName.attr1 + objName.attr2.to_s + objName.attr3.to_s)
Classes, attributes, and constructors:
class ClassName2 attr_accessor :attr1, :attr2, :attr3 def initialize(attr1, attr2, attr3) @attr1 = attr1 @attr2 = attr2.to_i @attr3 = attr3.to_f end def print puts (@attr1 + @attr2.to_s + @attr3.to_s) end end objName2 = ClassName2.new("Text",10,0.5) objName2.print
Parent and child classes:
class ClassName3 < ClassName def print puts (@attr1 + @attr2.to_s + @attr3.to_s) end end objName3 = ClassName3.new() objName3.attr1 = "Text" objName3.attr2 = 10 objName3.attr3 = 0.5 objName3.print
Creating modules (libraries) of functions:
module ModuleName def functionName puts "Executed!" end end include ModuleName ModuleName.functionName()
Importing/requiring functions from a module:
require "/home/user/file.rb" require_relative "file.rb" include ModuleName ModuleName.functionName()
Interactive Ruby
irb -v irb