From: Ross Bamford Date: 2006-03-25T19:47:20+09:00 Subject: Re: require a certain version of the ruby interpreter On Sat, 2006-03-25 at 19:08 +0900, Robert Klemme wrote: > Ross Bamford wrote: > > On Sat, 2006-03-25 at 11:06 +0900, dblack@wobblini.net wrote: > >> (What, no inject? :-) I don't think Array has >= though. > > Actually, a version with #inject occurred to me after I sent the other > one out when I thought about reducing redundancy in the code above - > you're also right about Array (strange though as it does implement <=>): > > def ev(ver) > raise "Requiring at least #{ver}" unless > [RUBY_VERSION,ver].map! {|v| v.scan(/\d+/).map! {|x|x.to_i}}. > inject {|v1,v2| (v1<=>v2)>=0} > end > Ahh, there it is - I knew inject would fit in here somewhere :) Array implementing <=> is a nice little thing, I guess then (again given no two digit versions as Matz has promised) it could be: def ev(v) raise "need #{v}" unless (VERSION.split('.') <=> v.split('.')) >= 0 end > > :) Even I couldn't find an excuse for inject on this one. The best I > > could do was: > > > > def ensure_version(v) > > raise "need #{v}" if VERSION.split('.').zip(v.split('.')).any? {|a,b| a < b} > > end > > Nice, too! I like the approach with any?. > > > (given that we don't need to worry about two-digit versions of course). > > You could still to the .to_i when comparing. True, but at 78 characters and the limit of whitespace decency, I couldn't make it fit ;) -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk