From: Abder-rahman Ali on
When I try for example to compare the following strings in Ruby, I get
"true".

puts 'Xeo' < 'ball'

When I make 'Xeo' start with a lowercase letter, i get 'false'

puts 'xeo' < 'ball'

The second statement is clear, but why when I capitalize 'Xeo' I get
true?

Thanks.
--
Posted via http://www.ruby-forum.com/.

From: Abder-rahman Ali on
Abder-rahman Ali wrote:
> When I try for example to compare the following strings in Ruby, I get
> "true".
>
> puts 'Xeo' < 'ball'
>
> When I make 'Xeo' start with a lowercase letter, i get 'false'
>
> puts 'xeo' < 'ball'
>
> The second statement is clear, but why when I capitalize 'Xeo' I get
> true?
>
> Thanks.

The "Learn to Program" book by Chris Pine mentions that computers order
capital letters as coming before lowercase letters. So, can it be
explained then by this?

Thanks.
--
Posted via http://www.ruby-forum.com/.

From: Jonathan Nielsen on
[Note: parts of this message were removed to make it a legal post.]

On Fri, Jun 18, 2010 at 11:46 AM, Abder-rahman Ali <
abder.rahman.ali(a)gmail.com> wrote:

> When I try for example to compare the following strings in Ruby, I get
> "true".
>
> puts 'Xeo' < 'ball'
>
> When I make 'Xeo' start with a lowercase letter, i get 'false'
>
> puts 'xeo' < 'ball'
>
> The second statement is clear, but why when I capitalize 'Xeo' I get
> true?
>
> Thanks.
> --
> Posted via http://www.ruby-forum.com/.
>
>
Because the '<' is doing a character-by-character compare on the strings.
As it turns out, 'X' < 'b' is true, while 'x' < 'b' is false. This is
because in the basic character set, the uppercase letters are lower-valued
than lowercase letters. See http://www.asciitable.com/

-Jonathan Nielsen

From: Kirk Haines on
On Fri, Jun 18, 2010 at 11:46 AM, Abder-rahman Ali
<abder.rahman.ali(a)gmail.com> wrote:
> When I try for example to compare the following strings in Ruby, I get
> "true".
>
> puts 'Xeo' < 'ball'
>
> When I make 'Xeo' start with a lowercase letter, i get 'false'
>
> puts 'xeo' < 'ball'
>
> The second statement is clear, but why when I capitalize 'Xeo' I get
> true?

Uppercase letters come before lowercase letters.

You can look at the implementation in the source (start at
rb_str_cmp()), but if you dig deeply enough, it comes down to the way
the standard C library function memcmp() works. It compares bytes. And
an ASCII 'X' is represented by a smaller value (88) than an ASCII 'b'
(98). So 'Xeo' is less than 'ball'.


Kirk Haines
Developer
Engine Yard

From: Josh Cheek on
[Note: parts of this message were removed to make it a legal post.]

On Fri, Jun 18, 2010 at 12:46 PM, Abder-rahman Ali <
abder.rahman.ali(a)gmail.com> wrote:

> When I try for example to compare the following strings in Ruby, I get
> "true".
>
> puts 'Xeo' < 'ball'
>
> When I make 'Xeo' start with a lowercase letter, i get 'false'
>
> puts 'xeo' < 'ball'
>
> The second statement is clear, but why when I capitalize 'Xeo' I get
> true?
>
> Thanks.
> --
> Posted via http://www.ruby-forum.com/.
>
>
Well, this used to be easy to show, but apparently since ascii has been
abandoned, and I don't know unicode, I have to resort to hacky things like
this to explain it.


$chars = (1..128).inject(Hash.new) { |chars,num| chars[num.chr] = num ;
chars }

def to_number_array(str)
str.split(//).map { |char| $chars[char] }
end

to_number_array 'Xeo' # => [88, 101, 111]
to_number_array 'xeo' # => [120, 101, 111]
to_number_array 'ball' # => [98, 97, 108, 108]
to_number_array 'ABC' # => [65, 66, 67]
to_number_array 'abc' # => [97, 98, 99]



In this case, $chars is a hash that will take a 1 character string, and
return its ascii value. So the method receives a String, and returns an
array where each index is the ascii value of the character.

Then to understand why one would be less than or greater than the other, go
through index by index, comparing the number in that index. If the two
strings (or in this case, their array representations that I made) have
different numbers, then whichever has the smaller number is considered less
than the other. If you run out of indexes on one of them, then that one
comes before the other. If you run out of indexes on them both
simultaneously, then they are equal.