Ruby is a dynamic typed language. This thing is important and you have to learn how to live with it to avoid logical errors. Dynamic typing means you can change the variable content and type during program execution. This may be unintentional. Usually is a bad practice to change data type of a variable and reuse variables for other purposes they are initially designed for.
Example
I can change data type for a variable! Wait… what ?
# declare integer variable
x = 1
puts "x = #{x}"
puts x.class # Integer
# changing data type for x variable
x = '?'
puts "x = #{x}"
puts x.class # String
Ruby has built-in data types you can use to define variables and attributes.
Literals are constants, created with alphanumeric symbols. Each data type has a specific literal notation that can be used to determine the type of variable. In Ruby you do not have to specify the type like you do in Java, C++ or Ada or Pascal. It is good enough to make an assignment using a literal.
In next example we use "assign operator" this will create an "assign statement".
a = 0 #integer
b = 0.5 #float
# define two strings
s1 = 'single quoted string'
s2 = "double quoted string"
puts s1
puts s2
h = true #boolean true
q = false #boolean false
n = nil #nothing at all
#define a symbol
x = :test
print "x=", x,"\n"
Note: In Ruby there is no keyword for "assign" statement. You start with an "identifier" of a variable that may exist or not. If the variable do not exist it will be created immediate and will have an initial value equal to the expression literal.
Homework: Open this example live and run it: ruby data types
Ruby has two built-in collections. You can create a collection using type inference with literals like in Python. The two literals are using two kind of brackets: square brackets for Array and squiggly brackets for hash map:
Examples
#array of integers
v = [1,2,3]
print "v=",v, "\n"
print "v[0]=", v[0], "\n" #first element
print "v[-1]=",v[-1], "\n"#last element
#array of strings
z = ['a','b','x']
print "z=",z, "\n"
#array of symbols
u = [:a,:b,:c]
print "u=", u, "\n"
#hash of strings
o = {'a' => 1, 'b' => 2, 'c' => 3}
print "o=", o, "\n"
#hash of symbols
q = {:a => 1, :b => 2, :c => 3}
print "q=", q, "\n"
Homework: Open this example live and run it: ruby collections
In Ruby you can not operate on different data types. There is a type safety built-in. However, some types are compatible and operator "==" is doing automatic conversion between float and integer type. Unlike JavaScript though you can not compare one string with one number.
Sometimes you must do explicit conversion between two data types. You can do this using "to_x" methods, where "x" can be "i", "f", "s" … depending on data type. For example let’s convert one number and concatenate the number with a string:
#convert integer to number
x = 10;
# use conversion and concatenation
puts x.to_s + "%"; # 10%
# alternative use string interpolation
puts "#{x}%"
Note: In the previous example we use string interpolation as alternative. Sometimes you can do one thing in many ways in Ruby. It depends on every team to develop a style. You have to be prepared though to understand other’s code so we sometimes offer an alternative.
Read next: Decision Statement