Ruby Data Types

gobias101
2 min readMar 24, 2022

--

Last week we went over all the different data types in JavaScript, this week we will do the same for Ruby so we can see just how similar different software languages are!

Ruby has 6 data types, differing from the 8 used by JavaScript (JS), though you will find them all very similar, lets start!

  1. Numbers. Just like JS, numbers in Ruby include both integers and floating point numbers. to declare an integer in a Ruby project, simply type:
x=10

2. String. Just like in JS, a string in Ruby is used to contain text, and wrapped in single or double quotes. To declare a string in ruby, you can type the following:

word="string"sentence='This sentence is also a string'

3. Boolean. Again, just like JS, the Boolean datatype has only the two true or false values and is used to store yes/no and compute comparisons. To create a simple boolean test in Ruby, you can do the following:

if true
puts "yes"
else
puts "no"
end

4. Hashes. This is where it starts to get a little different than JS, while JS has objects as a datatype, hashes are ‘objects’ in Ruby. They are used to store key value pair data and can hold all different data types. See an example of hashes below:

blogInfo = { "name" => "Mikes Blog",
"months active" => 12,
"still updated?" => true
}

5. Arrays. While JS has arrays, in Ruby, they are it’s own datatype. Arrays in Ruby hold multiple types of data like a hash, but they dont have a key. To create an array, you can type the following:

blogInfor = ["Mikes Blog", 12, true]

6. Symbols. The final data type in Ruby is symbols. Ruby treats symbols as ‘lightweight strings’ meaning they take up a very small size in the program. TO create a symbol, you use a colon then your identifier, such as:

:data

symbols cannot have any spaces, so usually an underscore(_) is used to separate words. Symbols in ruby represent static values and are unique.

I hope seeing this and the last blog shows just how similar different software languages are, it is much easier to learn another language once you are familiar with one.

--

--

gobias101
gobias101

No responses yet