small useless little classes...

S

Stu

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, rather than the convoluted bccwin32/mingw32
etc, etc, etc.

I was disappointed in what Ruby provides regarding the host system.
Telling you what it was compiled on, rather than what it is running
on etc.. :( Im sure uname -a functionality could have been built
into the interpreter with ease.

I have only been doing Ruby for less than a week so Im sure some
of the comp.lang.ruby experts show me the finer ways of doing
things?


There is only 1 perhipheral routine inside which is versionCompare
returning (-1, 0 or 1).

Here is some sample test code output of that;

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 pre 1.8.2...

Ive googled and never seen any RC ruby release numbering or non x/y/z
numbering but who knows.. maybe I can rip all that out and remove
some complexity assuming I wont run into any -RC or whatevers...

#
# Ruby Info, v0.1 - 20041111
# Stu George, http://mega-tokyo.com
#
# 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
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 = 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 = 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.to_i < bbVer.to_i
return -1
elsif aaVer.to_i > bbVer.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
end


-- Dark Fiber --
[FAQ] Write Your Own Operating System
http://www.mega-tokyo.com/osfaq2
3x3 Eyes Fanfiction Archive
http://www.mega-tokyo.com/pai
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top