Data types in Ruby represents different types of data like text, string, numbers, etc. All data types are based on classes because it is a pure Object-Oriented language. There are different data types in Ruby as follows:
- Numbers
- Boolean
- Strings
- Hashes
- Arrays
- Symbols
Numbers: Generally a number is defined as a series of digits, using a dot as a decimal mark. Optionally the user can use the underscore as a separator. There are different kinds of numbers like integers and float. Ruby can handle both Integers and floating point numbers. According to their size, there are two types of integers, one is Bignum and second is Fixnum.
Ruby
distance = 0 . 1
time = 9 . 87 / 3600
speed = distance / time
puts "The average speed of a sprinter is
|
The average speed of a sprinter is 36.474164133738604 km/h
Boolean: Boolean data type represents only one bit of information either true or false.
Ruby
if true
puts "It is True!"
else
puts "It is False!"
end
if nil
puts " nil is True!"
else
puts " nil is False!"
end
if 0
puts " 0 is True!"
else
puts " 0 is False!"
end
|
It is True!
nil is False!
0 is True!
Strings: A string is a group of letters that represent a sentence or a word. Strings are defined by enclosing a text within a single (”) or double (“”) quotes. You can use both double quotes and single quotes to create strings. Strings are objects of class String. Double-quoted strings allow substitution and backslash notation but single-quoted strings doesn’t allow substitution and allow backslash notation only for \\ and \’.
Ruby
puts " String Data Type";
puts 'escape using "\\"' ;
puts 'That\'s right' ;
|
String Data Type
escape using "\"
That's right
Hashes: A hash assign its values to its key. Value to a key is assigned by => sign. A key pair is separated with a comma between them and all the pairs are enclosed within curly braces. A hash in Ruby is like an object literal in JavaScript or an associative array in PHP. They’re made similarly to arrays. A trailing comma is ignored.
Ruby
hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
hsh. each do |key, value|
print key, " is ", value, "\n"
end
|
red is 3840
green is 240
blue is 15
Arrays: An array stores data or list of data. It can contain all types of data. Data in an array are separated by comma in between them and are enclosed within square bracket.The position of elements in an array starts with 0. A trailing comma is ignored.
Ruby
ary = [ "fred", 10 , 3 . 14 , "This is a string", "last element", ]
ary. each do |i|
puts i
end
|
fred
10
3.14
This is a string
last element
Symbols: Symbols are light-weight strings. A symbol is preceded by a colon (:). They are used instead of strings because they can take up much less memory. Symbols have better performance.
Ruby
domains = { :sk => "GeeksforGeeks", :no => " GFG ", :hu => "Geeks"}
puts domains[ :sk ]
puts domains[ :no ]
puts domains[ :hu ]
|
GeeksforGeeks
GFG
Geeks
Last Updated :
23 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...