Thursday, November 11, 2004
RUBY: RubyInfo.rb
I am writing a small app which needs to know what platform its running on and such, so I knocked out this small class. It lets me determine what platform it (should) be running on, returning either WIN32, UNIXLIKE, OS2 or AMIGA.
There is only 1 perhipheral routine inside which is versionCompare returning (-1, 0 or 1).
Here is some sample testing.
1.8.2 is higher than 1.8.2-rc2
1.8.2-rc2 is lower than 1.8.2
1.8.2-rc1 is lower than 1.8.2-rc2
1.8.2-rc3 is the same as 1.8.2-rc3
1.6.0 is lower than 1.8.2
1.8.2 is the same as 1.8.2
As you can see, RC versioning is lower in the chain than non RC. Since it is assumed 1.8.2-rc1 is LOWER than 1.8.2…
Class code follows…
#
# Ruby Info, v0.1 - 20041111
# Stu George, [url=http://mega-tokyo.com]http://mega-tokyo.com[/url]
#
# License : Public Domain
#
# Changelog
# v0.1 - 20041111
# - Initial mockup
# RubyInfo holds basic info on Version and Platform
# TODO
# - Detect running on Risc OS?
#
class RubyInfo
attr_reader :strPlatform, :strVersion
def initialize
# get ruby interpreter version
@strVersion = RUBY_VERSION
# Work out the platform
breakPlatformDown
# split it down
@intVersion = Array.new
breakVersionDown
end
# override
def to_s
puts "Version = #{@strVersion}, Platform = #{@strPlatform}"
end
# Compares two version strings
# 1.8.2rc1 is LOWER than 1.8.2
#
# returns -1 if ThisVer is LESS than ThatVer
# returns 0 if ThisVer is EQUAL to ThatVer
# returns 1 if ThisVer is GREATER than ThatVer
#
def versionCompare(thisVer, thatVer = @strVersion)
aVer = thisVer.to_s.split(".")
bVer = thatVer.to_s.split(".")
aaVer = Array.new
bbVer = Array.new
i = 0
aVer.each do |x|
x.scan(/w+/) do |z|
z.gsub!(/[^d]/, '').to_s
aaVer[i] = z.to_s.to_i
i += 1
end
end
i = 0
bVer.each do |x|
x.to_s.scan(/w+/) do |z|
# remove non digits from string : eg rc2 -> 2
z.gsub!(/[^d]/, '').to_s
bbVer[i] = z.to_s.to_i
i += 1
end
end
# compare aaVer to bbVer
i = 0
j = aaVer.length
if aaVer.length < bbVer.length
j = bbVer.length
end
while i < j
# no more in aaVer... but must still be some in bbVer.
# thus 1.8.2-rc2 is -1 to 1.8.2
if i >= aaVer.length
return 1
end
if i >= bbVer.length
return -1
end
if aaVer[i].to_i < bbVer[i].to_i
return -1
elsif aaVer[i].to_i > bbVer[i].to_i
return 1
else
i += 1
end
end
return 0
end
private
# breakPlatformDown creates our platform string.
# WIN32
# *-mswin32
# *-mingw32
# *-bccwin32
# *-msdosdjgpp
#
# OS2
# *-os2
# *-os2-emx
# *-emx-os2
#
# AMIGA
# amigaos
#
# UNIXLIKE (we dont test this, its the default)
# *-cygwin
# *-darwin
# *-aix
# *-linux
# *-freebsd
# *-netbsd
# *-openbsd
# *-solaris
# *-hpux
#
def breakPlatformDown
x = RUBY_PLATFORM
# the GROSS assumption here is RUBY_VERSION returns
# the platform the binaries were built with, not
# what it is running under...
# easiest thing to do is to determine unixlike exceptions.
# aka everything that isnt a unixlike.
y = x.scan(/(mswin32|mingw32|bccwin32|msdosdjgpp|os2|amigaos)/)
# TODO : how do we test for riscos?
# NOTES : os2 also covers os2-emx (eg: i386-pc-os2-emx)
case y.to_s
when "mswin32", "mingw32", "bccwin32", "msdosdjgpp"
@strPlatform = "WIN32"
when "os2"
@strPlatform = "OS2"
when "amigaos"
@strPlatform = "AMIGA"
else
@strPlatform = "UNIXLIKE"
end
end
def breakVersionDown
aVer = @strVersion.split('.')
i = 0
aVer.each do |x|
x.scan(/w+/) do |z|
# remove non digits from string : eg rc2 -> 2
z.gsub!(/[^d]/, '').to_s
@intVersion[i] = z.to_i
i += 1
end
end
end
end
Filed Under : Development •
Tags:
(3) Comments