I was dealing with a problem earlier on today that required me to  
check software versions against each other in the form of '1.2.3.4'  
and '1.86.13' etc. and came up with this code to do it:

# bigger? '1.3.1.1', '1.4'

def bigger?(a,b)
   a_parts = a.split '.'
   b_parts = b.split '.'
   (a_parts.size > b_parts.size ? a_parts.size : b_parts.size).times {| 
i|
     if (a_parts[i].to_i || 0) > (b_parts[i].to_i || 0)
       return(true)
     end
     if (a_parts[i].to_i || 0) < (b_parts[i].to_i || 0)
       return (false)
     end
   }
   return false
end
(mirrored here: http://gist.github.com/108740)

The code will return true only if software version a is higher than  
software version b. It will return false if the versions are matched  
of if version b is higher.

And I figured it might be fun to send it out and see if people could  
come up with a shorter/better/more exciting way of doing this. It is  
Friday after all, party on ;)

Will.

-- 
Will Jessop
Super Shiny Robot Limited - Professional web design and development

t: 07939 547 962
w: http://supershinyrobot.com/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"NWRUG" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nwrug-members?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to