Mike Chaliy Mike Chaliy - 1 year ago 71
Ruby Question

Compare number and its string representation

val1 = 1
val2 = "1"

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

How to compare number and its string representation?

Answer Source

Convert either to the other, so either:

val1.to_s == val2 # returns true


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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download