DEV Community

Akira
Akira

Posted on • Originally published at Medium on

Basic Data Types in Ruby

As a beginning programmer, it’s important to get familiar with data types as soon as possible. Getting to know them first is a great way to start with a bottom-up approach, as opposed to thinking of data structures first, or even classes and instances of said class, because, for a beginner, those terms can be completely foreign and potentially off-putting.

So, to start with the bare bones basics, with a bottom-up approach, here are some basic data types in Ruby.

Integers

Integers are whole numbers (numbers without a decimal).

Examples:

1

7

-21

3_000

You can do math in Ruby with these, using the same operators you did in grade school.

1 * 7

=> 7

4 + 10

=> 14

8–5

=> 3

45 % 10

=> 5

(note: When using the % sign, its called the modulo operator; the modulo operator returns the remainder.)

6 / 3

=> 2

3 / 4

=> 0

(note: Of course we know 3 divided by 4 is not zero, but since we are using the integer datatype for both numbers, Ruby returns an integer. The actual answer would need to be rendered as a “float” datatype, which we will get to next.)

Floats

Numbers with decimals.

Examples:

5.5

-2.75

9.30

You can do math with these too, oh boy! What do you notice about these return values?

4.2 + 5.3

=> 9.5

3.0–5

=> -2.0

3.0 / 4.0

=> 0.75

(note: Remember earlier when we used integers exclusively to perform this same function, an integer was returned? Now that we are using floats, a float is returned, which also happens to be mathematically correct!)

3.0 / 4

=> 0.75

(note — You don’t need both numbers to be a float to return a float. Ruby returns a float even if only one of the datatypes used in an equation is a float.)

Strings

Strings are characters between quotation marks. The quotes can be double or single, but they need to match.

“12345”

‘Hi new friend, my name is Akira. Let’s get this bread.’

“n0 he d!d naW+”

You can do some pretty cool stuff with strings, a good place to start is with interpolation.

To do so, follow these steps in Erb or pry:

First, set a variable that holds someone’s name as a string.

Then, make a new string and put the variable #name inside of it.

Look at the return value!

Example:

name = “Akira”

“Hi new friend, my name is #{name}”

=> “Hi new friend, my name is Akira”

Cool, huh?

There are all kinds of cool things to do with strings, but that’s another post for another time.

Arrays

Arrays are for me, where it starts to get seriously cool.

They are comma-separated, ordered lists, enclosed in square brackets.

They can hold lots of pieces of data (called elements), or be totally empty.

They can hold any type of data and that data type can be mixed (some strings, some integers, even other arrays!). This is generally frowned upon as it’s not the best use of an array, but it *can* be done.

Note: by “ordered”, I mean the elements stored in an array are indexed in order. They start at 0 (I know, wack) and then from there go 1, 2, 3, 4, 5, 6, etc.

Examples of arrays:

[]

[1, 2, 4, 5, 6, 7]

[7.9, 8.98, 0.25]

[“Kathryn”, “Rachyl”, “Bryan”]

[[0,1], [2, 9], [6, 98], [34, 876] ]

[“seventeen”, 17, 17.0, [17] ]

There are so many amazing ways to utilize arrays, which I’ll write another post on at another time.

Alright, there we have it! A quick and dirty overview of very common Ruby datatypes. There are many cool things to do with all of these — happy exploring!

Top comments (0)