By Md. Sabuj Sarker | 11/22/2017 | General |Beginners

Understanding Data Types in Ruby

Understanding Data Types in Ruby

The foundation of computer systems lies in the cycle of input, processing and output. In our programs we work with data and provide output or feedback in various forms. Data is classified in different categories and we call these categories data types. To work with data of different categories, we need to know their types. Think of a real life situation. You add 2 and 2 to make 4. 2 is a number and the result 4 is a number too. But you cannot add 2 and a human—this does not make any sense. In a similar fashion, computers also need to know which category some data falls in to work with it.

Ruby is not a statically typed language like C or Java. But that doesn't mean that data in Ruby is type-less. If that were the case Ruby could not provide different types of outputs as a result of different operations. We call Ruby a duck typed language.

Getting Started

To get started with the this guide and the examples shown here you should have Ruby installed on your system. You should also have some moderate experience using command line programs. You must have a plain text editor or a code editor to edit code with. You can use IDEs if you want, but that is not recommended at this stage of learning.

To prepare your environment, create or choose a directory where you want to save your Ruby files. Start your command line in this directory or point your command line's working directory to this one. Create a file called ruby_types.rb. Run the file by invoking ruby ruby_types.rb on the command line to check if everything is alright.

Integers

Integers are numbers without decimal points. Integers can be negative or positive. We also refer to Integers as int. Write integers numbers as you write them in real life. To make a negative number, precede that with a negative sign (-).

i = 55
puts i

Outputs:

55

In real life we use commas as thousand separators. For example: 9,999,999. But, we cannot write it like this in Ruby, which has another way of accomplishing this. We can use underscores (_) instead of commas. Remember that you will not see any underscore when you convert that integer to a string or print out the string. That means that those underscores have nothing to do with integer other than for decorative purpose or clarity.

i = 9_999_999
puts i

Outputs:

9999999

Again, it is not a must that you use underscores as thousand separator. You can use it between each digits if you want.

i = 9_9_9_9_9_9_9
puts i

Outputs:

9999999

Floats

Floats are the floating point numbers. In everyday math we call them decimal numbers, though this is not an exact definition of floating point number. Floating point numbers can be both rational and irrational. You can create floating point numbers by placing a decimal as a dot inside digits.

i = 10.5
puts i

Outputs:

10.5

We can write floating point numbers as scientific notations. We need to place an e inside consecutive digits with an optional + or - followed by it.

i = 10.5e-1
puts i

Outputs:

1.05

Booleans

Booleans in programming indicate either a true or false value. Booleans are results of various boolean operations and can also be put literally as true or false.

Boolean operators include >, <, >=, <=, ==. Let's demonstrate with examples.

puts 1 > 0
puts 5 < 7
puts 3 == 3
puts 3 >= 4
puts 8 <= 7

Outputs:

 

true

true

true

false

false

Strings

Strings in Ruby are a sequential collection of characters. Strings can be created by surrounding a set of characters with a pair of single quotes or double quotes. Here doc style strings can also be used. Strings can also be returned from various methods and actions in Ruby. Unlike many other languages, strings in Ruby are Unicode aware.

I am not going to go deep into the rabbit hole in this post as I have covered various aspects of strings in few other articles.

Arrays

Arrays are a sequential collection of objects. Arrays in Ruby can contain any type of object unlike in other languages. To create an array put the objects or values, separated by commas, inside a pair of square brackets.

s = "Hello"
arr = [s, 5, "world", 55.66, nil]
print arr

Outputs:

["Hello", 5, "world", 55.66, nil]

Array elements can be accessed by numeric indexes. In other words, index is the position in an arrays. In arrays, the index or position starts from zero (0).

Unlike many other modern languages, arrays in Ruby are not of fixed length and can be resized dynamically in the runtime.

Symbols

A symbol in Ruby is a very special type of data and is immutable. Symbols are more like variable names preceded by a colon. Instead of being a variable these are values and can be assigned to a variable.

s = :a_symbol
print s

Outputs:

a_symbol

No matter how many time you refer to a symbol in a Ruby program, the existence of a symbol is only once. A symbol is present in only one position in memory. It is widely used for keys of hashes and as method parameters. In a future article I will discuss symbols in details.

Hashes

Hash is a data structure of key value pairs. Values are added and accessed by keys in hashes. In arrays we access or modify values by indexes and in hashes we access and modify values by keys. Key value pairs are separated by commas and the pairs are surrounded by curly braces. Key and value is separated by =>. Let's see an example:

h = {"name"=> "Md. Sabuj Sarker", :email=> "md.sabuj.sarker@gmail.com", "position":"Software Engineer", 0=>0}
print h

Outputs:

{"name"=>"Md. Sabuj Sarker", :email=>"md.sabuj.sarker@gmail.com", :position=>"Software Engineer", 0=>0}

Note:

Keys and values in hashes can be of different types as demonstrated in the example above. It contains keys of strings, symbols and number. It may contain other types of keys.

Conclusion

Ruby is a very flexible language and we do not need to declare data types. It works well until we do right thing with the right type of data. Every data has associated type with it. If you need to check the types of some data in runtime you can do this by invoking the class method on objects. Keep coding with different types of data to be better acquainted with them.

Remember to stop by the homepage for tons of SDKs, Libraries, and other Dev Tools.

By Md. Sabuj Sarker | 11/22/2017 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now