Basics of Ruby: Part 1 (Data Types/Data Storage)

Part 1 (Data Types/Data Storage)

Ruby is a dynamic, general-purpose programming language created by Yukihiro "Matz" Matsumoto in Japan around the mid-90's. It has many uses but with it's flexibility, it makes a great language to write exploits in. In fact, the entire Metasploit Framework is written in Ruby! The sole purpose of this series is to teach hackers the basics of Ruby, along with some more advanced concepts that are important in hacking.

Get Ruby

Before we can work with Ruby, we need to download it. Kali Linux, and most hacking distros have it preinstalled, but if you don't have it, use your package manager. For Ubuntu, you need to do the following:

sudo apt-get install ruby-full
This will download & install ruby

Data Types and Storage in Ruby

In Ruby, there are 4 data types:
1) Numbers (1,2,3,4,5...)
2) Strings ("Null Byte", "Ruby")
3) Boolean Values (true, false)
4) nil (Ruby version on Null)

Having all this information is great, but how are we going to use it if we can't store it? That is where the Data storage comes into place!

1) Variables

Variables are words/letters that hold some type of data. These are the simplest of the storage's. They are defined by putting the letter/word equal to a number, string, Boolean, or even a method! Examples:

2) Arrays

Simply put, arrays are lists of different data. While variables hold only one value, arrays can hold however many you please of any data type you want. These are defined by a letter/word that are equal to brackets ([]). You can access the values by typing the array name along with the index of the value. For example:

3) Hashes

Hashes are the Ruby equivalent to Python's dictionaries. They store a key/value pair. There are two ways to define them. The first way:

The second way:

You can access them by calling the name of the hash with the key in brackets...

Next Time...

Basics of Ruby: Part 2 (loops) will be out sometime by the end of this week. I am becoming very busy as this is the last day of my summer break, but my plans have two lessons being published a week.

7 Comments

EDIT: Pictures are up... (i forgot to upload them the first time...)

Good article. Did you learn Ruby from Codecademy by any chance?

Yeah. Then I checked out some other tutorials.

Cool.

I was thinking of learning the basics of Ruby on Codecademy before buying some more advanced books since I learnt the basics of Python on there. Do you recommend the course on Codecademy or is there a better one out there, based on your experiences?

From my experiences, Codecademy was best to learn the basics. Other courses only covered half of what codecademy covered.

This is really good, but I would like to see more tutorials like the one about using sockets in ruby, because you showed how to use them to actually create something usefull.

Don't worry. We'll get there. First I want to focus on the basics.

Share Your Thoughts

  • Hot
  • Latest