lundi 2 mars 2015

Comparisons on numbers and strings in ruby

In console:



1.9.3p547 :010 > my_s = 00013.to_s
=> "11"
1.9.3p547 :011 > my_s = 00013.to_i
=> 11
1.9.3p547 :012 > 13.to_s
=> "13"
1.9.3p547 :013 > 13.to_i
=> 13


Why this difference in treatment? How can comparisons be made between string values that are fundamentally numbers but that have been padded with zeros?


Aucun commentaire:

Enregistrer un commentaire