Comparing two lines using> (more than a character) in Ruby?

I came across a piece of code in a project that I'm working on that looks scary. It is assumed that it displays a +/- delta between two numbers, but uses >strings to compare numbers instead of numbers.

I assume the code is working as expected at the moment, so I'm just trying to figure out how Ruby compares these strings in this case.

Here is an example of variable substitution:

if '55.59(100)' > '56.46(101)'
  delta = '+'
else
  delta = '-'
end
+3
source share
2 answers

String Comparable, <, >, >= .. (<=>). , a b, a <=> b -1, < true. <=> , , "" .

+4

, . ( , -...)

irb(main):001:0> '44' < '45'
=> true
irb(main):002:0> '44.123(whatever)' < '99.921(bananas)'
=> true

irb(main):003:0> '44.123' < '100'
=> false
irb(main):004:0> '44.123' < '9.123'
=> true

, , . , ( ).

+8

All Articles