Mike Chaliy Mike Chaliy - 2 months ago 6
Ruby Question

Compare number and its string representation

val1 = 1
val2 = "1"

if val1 == val2 #< Question is in this line
end


How to compare number and its string representation?

Answer

Convert either to the other, so either:

val1.to_s == val2 # returns true

Or:

val1 == val2.to_i # returns true

Although ruby is dynamically typed (the type is known at runtime), it is also strongly typed (the type doesn't get implicitly typecast)

Comments