From: dblack@... Date: 2006-07-10T00:01:57+09:00 Subject: Re: Sorting arrays Hi -- On Sun, 9 Jul 2006, Dark Ambient wrote: > On 7/9/06, dblack@wobblini.net wrote: >> >> def sort(list) >> sorted = [] >> unsorted = [] >> >> lowest = nil >> >> list.each do |item| >> puts "Comparing #{item.inspect} (item) to #{lowest.inspect} (lowest)" >> if lowest == nil || item < lowest >> puts "Either lowest is nil, or #{item.inspect} is less than #{ >> lowest.inspect}" >> puts "So lowest is now being set to #{item.inspect}" >> lowest = item >> end >> end >> >> So the bottom line here is "anything" in the array (except perhaps for an >> empty element) is less then nil ? > > That being the case I now understand how it's working, but don't > understand nil, at least in the way it works in a comparison. > Maybe it's just something I need to accept :) > a < b , 3 < 5 , those all seem clear, even a < 1. > oh well. The only comparison with nil that you're doing is: lowest == nil The code on the right of the || only gets executed if lowest == nil is false. So when you do: item < lowest lowest is already known not to be nil. Maybe the missing piece of the puzzle is this: When you have: if expr1 || expr2 expr2 is never reached if expr1 is true. For example: if 2 + 2 == 4 || 1/0 == 10 puts "One of them is true!" end If 1/0 == 10 were to be executed, you would get a ZeroDivisionError -- but you don't, because 2+2==4 is true. That's enough to satisfy the "or" condition, so Ruby doesn't waste time testing the part on the right. David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy http://www.manning.com/black => RUBY FOR RAILS, the Ruby book for Rails developers http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log dblack@wobblini.net => me