Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - TYPE CASTING


So far we have seen, how to add two numbers that are of the same type.


i.e. We have added two numbers that are of Data Type Fixednum.


Example :



x = 5 
y = 6
z = x + y
puts "The added value is : #{z}"  
puts "The data type of x is : #{x.class}"
puts "The data type of y is : #{y.class}"
puts "The data type of z is : #{z.class}"


Output :



  The added value is : 11
  The data type of x is : Fixnum
  The data type of y is : Fixnum
  The data type of z is : Fixnum

So, if we see the above output, the Data Type of x and y is Fixnum because both have an integer value in it.


x = 5
y = 6

Similarly, if we see the Data Type of the variable z, it is also Fixnum. Because z holds the added value of the variables x and y. Which is also an integer(i.e. 11).


Now, there might also be a scenario where you want to add a floating point number and a whole number.


i.e. We want to add the whole number 5 and a floating point number 6.5.


So, what would be the output and most importantly, what would be the Data Type of the variable that would be holding the added value.


Let us see in the below example.


Example :



x = 5 
y = 6.5
z = x + y
puts "The added value is : #{z}"  
puts "The data type of x is : #{x.class}"
puts "The data type of y is : #{y.class}"
puts "The data type of z is : #{z.class}"


Output :



  The added value is : 11.5
  The data type of x is : Fixnum
  The data type of y is : Float
  The data type of z is : Float

Well! If we see the output, the added value of 5 and 6.5 is 11.5.


Now, if we see the Data Types of all the variables :


For variable x, it is Fixnum.
For variable y, it is Float.

And for the variable z it is Float as well. Because the added value is 11.5 and as usual, Ruby made an intelligent guess that the Data Type of z should be Float.


So, the result of a Fixnum and Float is a Float, which is decided by Ruby.


But, let's say, we don't want the added value of 5 and 6.5 to be 11.5. Rather we want it to be just 11.


This is where Type Casting comes into picture.


Let us rewrite the above program with Type Casting.


Converting a float Data Type to an int Data Type


Example :



x = 5 
y = 6
z = (x + y).to_i
puts "The added value is : #{z}"  
puts "The data type of x is : #{x.class}"
puts "The data type of y is : #{y.class}"
puts "The data type of z is : #{z.class}"


Output :



  The added value is : 11
  The data type of x is : Fixnum
  The data type of y is : Fixnum
  The data type of z is : Fixnum

Now, if we see the output, we can see the added value of 5 and 6.5 is 11 and not 11.5.


The added value is :  11

And the Data Type of the variable that holds 11 is Fixnum.


The data type of z is : Fixnum

And how did we achieve this?


Well! By Type Casting.


At the time of adding the numbers, we tell Ruby that the added value should be of Data Type Fixnum and not Float.


And we have used the Type Converter .to_i.


z = (x + y).to_i

The Type Converter operator .to_i checks the value inside its brackets(x + y in this case). And converts it into Data Type Fixnum.


Similarly, we have Type Convertors like to_f and to_s that are used to covert other Data Types to Float and String.


Converting an Fixnum Data Type to an Float Data Type


Example :



x = 5.to_f
puts "The value of x is : #{x}"  
puts "The data type of x is : #{x.class}"


Output :



  The value of x is : 5.0
  The data type of x is : Float

As we know 5 is of Data Type Fixnum. And we have used the type converter .to_f,


x = 5.to_f

To convert the value 5 to Float.


And if you see the output, it's 5.0 and the Data Type is Float.


Converting an String Data Type to an Fixednum or Float Data Type


Say, there is a scenario, where we have assigned a number in String format.


Let us clear with the below example.


Example :



x = 5 
y = "6"
z = x + y
puts "The added value is : #{z}"  
puts "The data type of x is : #{x.class}"
puts "The data type of y is : #{y.class}"
puts "The data type of z is : #{z.class}"


Output :



  add.rb:3:in +: String can't be coerced into Fixnum (TypeError)
  from add.rb:3:in <main>

So, the output tells us a lot! But the moral of the story is, we have ended up with an error.


And that is because we have tried adding a String(Note the double quotes "" around 6),


y = "6"

And a number,


x = 5

And we just ended up with the error.


in +: String can't be coerced into Fixnum (TypeError)

Now, let us fix the above example with Type Casting.


Example :



x = 5 
y = "6".to_i
z = x + y
puts "The added value is : #{z}"  
puts "The data type of x is : #{x.class}"
puts "The data type of y is : #{y.class}"
puts "The data type of z is : #{z.class}"


Output :



  The added value is : 11
  The data type of x is : Fixnum
  The data type of y is : Fixnum
  The data type of z is : Fixnum

So, we have used the Type Convertor .to_i to convert the String to Fixnum.


y = "6".to_i

And 6 is treated as a number and we were able to add them.