[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

require a certain version of the ruby interpreter

Shea Martin

3/24/2006 8:36:00 PM

I would like to enforce ruby 1.8.4 or higher.

if VERSION < "1.8.4"
raise "Invalid version"
end

Fails when VERSION is "1.8.10".

Yeah, I know I could write my own string comparison, but was wondering
if there is a canned solution for this?

~S
14 Answers

Shea Martin

3/24/2006 9:04:00 PM

0

Shea Martin wrote:
> I would like to enforce ruby 1.8.4 or higher.
>
> if VERSION < "1.8.4"
> raise "Invalid version"
> end
>
> Fails when VERSION is "1.8.10".
>
> Yeah, I know I could write my own string comparison, but was wondering
> if there is a canned solution for this?
>
> ~S


This works nicely, but I am sure something like this is already built
in, but I can't seem to find it.

def require_version( p_ver_str )
l_have = VERSION.split('.')
l_need = p_ver_str.split('.')
l_need.each_index do |i|
if l_have[i].to_i < l_need[i].to_i
raise ScriptError, "Required Ruby #{p_ver_str}, found Ruby
#{VERSION}"
end
end
end

~S

Jim Weirich

3/24/2006 9:12:00 PM

0

Shea Martin wrote:
> I would like to enforce ruby 1.8.4 or higher.
>
> if VERSION < "1.8.4"
> raise "Invalid version"
> end
>
> Fails when VERSION is "1.8.10".
>
> Yeah, I know I could write my own string comparison, but was wondering
> if there is a canned solution for this?

if Gem::Version.new(VERSION) < Gem::Version.new("1.8.4")
raise "Invalid Version"
end

Or, if you want to get fancy, you could specify any version greater than
or equal to 1.8.4, but less than 1.9.

r = Gem::Requirement.new("~> 1.8.4")
unless r.satisfied_by?(Gem::Version(VERSION))
raise "Invalid version, must be #{r}"
end

Just make sure that rubygems is loaded to use this.

--
-- Jim Weirich



--
Posted via http://www.ruby-....


Dave Burt

3/24/2006 9:48:00 PM

0

Shea Martin wrote:
>I would like to enforce ruby 1.8.4 or higher.
>
> if VERSION < "1.8.4"
> raise "Invalid version"
> end
>
> Fails when VERSION is "1.8.10".

No, it doesn't, because there will never be a version "1.8.10". You don't
have to worry about this problem, Ruby versions will be string-comparable
for the forseeable future.

Cheers,
Dave


Robert Klemme

3/24/2006 9:49:00 PM

0

Shea Martin wrote:
> This works nicely, but I am sure something like this is already built
> in, but I can't seem to find it.
>
> def require_version( p_ver_str )
> l_have = VERSION.split('.')
> l_need = p_ver_str.split('.')
> l_need.each_index do |i|
> if l_have[i].to_i < l_need[i].to_i
> raise ScriptError, "Required Ruby #{p_ver_str}, found Ruby
> #{VERSION}"
> end
> end
> end

Hmmm, lettsee whether we can compress that a bit. How about:

def ensure_version(ver)
raise "Requiring at least #{ver}" unless
RUBY_VERSION.scan(/d+/).map! {|x|x.to_i} >=
ver.scan(/d+/).map! {|x|x.to_i}
end

Not really a one liner...

robert

Ross Bamford

3/25/2006 9:49:00 AM

0

On Sat, 2006-03-25 at 11:06 +0900, dblack@wobblini.net wrote:
> Hi --
>
> On Sat, 25 Mar 2006, Robert Klemme wrote:
>
> > Shea Martin wrote:
> >> This works nicely, but I am sure something like this is already built in,
> >> but I can't seem to find it.
> >>
> >> def require_version( p_ver_str )
> >> l_have = VERSION.split('.')
> >> l_need = p_ver_str.split('.')
> >> l_need.each_index do |i|
> >> if l_have[i].to_i < l_need[i].to_i
> >> raise ScriptError, "Required Ruby #{p_ver_str}, found Ruby
> >> #{VERSION}"
> >> end
> >> end
> >> end
> >
> > Hmmm, lettsee whether we can compress that a bit. How about:
> >
> > def ensure_version(ver)
> > raise "Requiring at least #{ver}" unless
> > RUBY_VERSION.scan(/d+/).map! {|x|x.to_i} >=
> > ver.scan(/d+/).map! {|x|x.to_i}
> > end
> >
> > Not really a one liner...
>
> (What, no inject? :-) I don't think Array has >= though.

:) 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

(given that we don't need to worry about two-digit versions of course).

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk



Robert Klemme

3/25/2006 10:04:00 AM

0

Ross Bamford wrote:
> On Sat, 2006-03-25 at 11:06 +0900, dblack@wobblini.net wrote:
>> Hi --
>>
>> On Sat, 25 Mar 2006, Robert Klemme wrote:
>>
>>> Shea Martin wrote:
>>>> This works nicely, but I am sure something like this is already built in,
>>>> but I can't seem to find it.
>>>>
>>>> def require_version( p_ver_str )
>>>> l_have = VERSION.split('.')
>>>> l_need = p_ver_str.split('.')
>>>> l_need.each_index do |i|
>>>> if l_have[i].to_i < l_need[i].to_i
>>>> raise ScriptError, "Required Ruby #{p_ver_str}, found Ruby
>>>> #{VERSION}"
>>>> end
>>>> end
>>>> end
>>> Hmmm, lettsee whether we can compress that a bit. How about:
>>>
>>> def ensure_version(ver)
>>> raise "Requiring at least #{ver}" unless
>>> RUBY_VERSION.scan(/d+/).map! {|x|x.to_i} >=
>>> ver.scan(/d+/).map! {|x|x.to_i}
>>> end
>>>
>>> Not really a one liner...
>> (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

> :) 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.

Kind regards

robert

Ross Bamford

3/25/2006 10:47:00 AM

0

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



dblack

3/25/2006 12:00:00 PM

0

Simon Kröger

3/25/2006 12:11:00 PM

0

>> [...]
>> :) 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?.

Yeah, me too. But its a bit buggy:

v = '1.8.5'
p '1.8.4'.split('.').zip(v.split('.')).any? {|a,b| a < b}
#=> true

v = '0.0.9'
p '1.8.4'.split('.').zip(v.split('.')).any? {|a,b| a < b}
#=> true

>> (given that we don't need to worry about two-digit versions of course).

we could just use string compare, right? (no inject, no any?, no fun of
course :))

what about

VERSION.gsub(/(\d+)/){$1.to_i.chr} >= v.gsub(/(\d+)/){$1.to_i.chr}

this works up to version 255.255.255 *g*

cheers

Simon


Pit Capitain

3/25/2006 12:13:00 PM

0

dblack@wobblini.net schrieb:
> Here's yet another variation on the theme. (I thought I'd posted this
> in my previous post but I didn't.)
>
> require 'scanf'
> def ensure_version(need)
> need.scanf("%d.%d.%d").
> zip(RUBY_VERSION.scanf("%d.%d.%d")).any? do |a,b|
> a > b
> end and raise "Requires at least #{need}"
> end

Both the solutions using any? aren't correct:

puts RUBY_VERSION # => 1.8.4
ensure_version "1.6.8" # => Requires at least 1.6.8 (RuntimeError)

Regards,
Pit