Comparing two strings using > (greater than sign) in Ruby?

compare, ruby, string

Solution

`String` includes the `Comparable` module, which defines `<`, `>`, `>=`, etc, based on the base class's compare (`<=>`) method. So if string a comes alphabetically prior to string b, `a <=> b` returns `-1`, and `<` returns `true`. The same `<=>` method is used for sorting strings, so you can imagine that in a sorted array of strings, each string is 'less than' its neighbor to the right.

Problem

I came across a piece of code in a project I'm working on that looks kind of scary. It's supposed to be displaying a +/- delta between two numbers, but it's using a `>` to compare strings of numbers instead of numbers. I'm assuming that the code is working as expected at the moment, so I'm just trying to understand how Ruby is comparing these strings in this case. Here's an example with the variables replaced: ``` if '55.59(100)' > '56.46(101)' delta = '+' else delta = '-' end ```

Original source