Ruby Data Types

Background

When we start learning a programming language, we generally start by learning about data types, variables, operators, conditionals, looping constructs and then into more of scripting/oops principles. In this post, let’s see what Ruby provides in terms of data types. This is just a fundamental class, so applying the concepts goes far beyond this post as the touch points with various other concepts will create more permutations and combinations of how they play out. But we should start somewhere.

Data Types

Integers

Ruby supports two types of integers: Fixnum that fall in range [-230 to 230-1 OR -262 to 262-1]. Bignum that falls outside this range. As you might have noticed, these data types are objects of the classes Fixnum or Bignum. You can verify this by assigning an integer value to a variable and printing its .class value

Float

Float numbers are of class Float. So in general when you assign a variable 123.4 for example, ruby automatically creates an object of Float class.

String Literals

String literals are generally enclosed within single quotes and are immutable. The only escape character that is allowed is backslash.

String

Strings belong to String class and enclosed within double quotes. They are mutable and allow substitution and backslash. Also allow evaluation of expression using #{}.

String class has many methods and it is beyond the scope of this post to talk about all methods, but definitely worth checking as many string manipulations using Ruby look artistic. Also by default String is an array i.e. you can access the characters using index. Read more here

Array

Arrays are generally comma separated list of objects between [ and ] or defined using Array.new. Ruby Arrays have lots of interesting methods like accessing, manipulating, sorting, searching etc. Read here

Range

Range as the name suggests is a series of objects with start and end. Defined using (start..end). This is generally where range is a sequence. Range as conditions also is very interesting. And finally there is Range as Intervals. This tutorial gives a nice overview.

Hash

A hash is a key,value pair. In Ruby key => value is how a pair in has is represented. From Ruby 1.9, we can also define key: value, which is another way to represent a hash pair. Generally declared with {key1:value1,key2:value2,…}. Keys are generally strings and value can be any object type. Read here

Date & Time

There is Date class. There is Time Class and there is DateTime class. Please click those links to read about the methods and operations available on those classes.

Closing Thoughts

We did not go into details of each data type above as the links provided are good enough to go through and understand the concepts. I did not want to repeat it on this page. Also bear in mind, that some are derived data types in the above list for eg. a hash is a derived data type. The reason I mixed them up was because generally we used any of the above data types during automation.