i’m following the “learning ruby” path, i’ve found this list very usefull the
last time (tnx), so i thought to send this simple example to better
understand what’d to be a good programming style.
···
expand -t 2 best_vv
class String
def newer?(s)
b = self.dup
b.sub!(/(\w±)/, ‘’)
t = $+
s.sub!(/(\w±)/, ‘’)
return -1 if t != $+ # not the same package
while (b.sub!(/(\d+.|\d+)/, ‘’)) do
t = $+.chomp(‘.’)
s.sub!(/(\d+.|\d+)/, ‘’)
return 1 if $+ == nil
return 1 if t.to_i > $+.chomp('.').to_i
end
return 0
end
end
class Array
def best_version?
s = 0
for i in 1…self.length() - 1 do
t = self[i].newer? self[s]
if t == 1
s = i
elsif t == -1
return nil
end
end
return s
end
end
vv = [‘ruby-2.2.5’, ‘ruby-3.2.6’, ‘ruby-5’]
i = vv.best_version?
puts vv[i]
how would you write or change this?
tnx again for your time
best regards,
saiph
–
here are more things in heaven and earth,
horatio, than are dreamt of in your philosophy.
vv = ['ruby-2.2.5', 'ruby-3.2.6', 'ruby-5']
i = vv.best_version?
how would you write or change this?
Well, like said previously it's faster to do it if all your numbers have
only one digit.
def newer?(s)
def best_version?
generally a method with ? at the end, return true or false perhaps best to
change the name.
Something like this (not tested)
svg% cat b.rb
#!/usr/bin/ruby
class String
REG = /\A\w+-(?=[\d.]+\z)/
def newer(s)
return nil unless self[REG] == s[REG]
a = self.scan(/(\d+)\.?/).flatten
b = s.scan(/(\d+)\.?/).flatten
a.zip(b) do |x, y|
t = x.to_i <=> y.to_i
return t unless t.zero?
end
0
end
end
class Array
def best_version
inject do |res, elem|
t = res.newer(elem)
return nil if t.nil?
>=0 ? res : elem
end
end
end
vv = ['ruby-2.2.5', 'ruby-3.2.6', 'ruby-5']
puts vv.best_version
svg%
My opinion about programming style is: make things as
simple as you can. Don’t create extra helper classes
etc if you don’t need them.
Kristof
···
On Fri, 23 Apr 2004 21:17:14 +0900, saiph wrote:
hi list,
i’m following the “learning ruby” path, i’ve found this list very usefull the
last time (tnx), so i thought to send this simple example to better
understand what’d to be a good programming style.
expand -t 2 best_vv
class String
def newer?(s)
b = self.dup
b.sub!(/(\w±)/, ‘’)
t = $+
s.sub!(/(\w±)/, ‘’)
return -1 if t != $+ # not the same package
while (b.sub!(/(\d+.|\d+)/, ‘’)) do
t = $+.chomp(‘.’)
s.sub!(/(\d+.|\d+)/, ‘’)
return 1 if $+ == nil
return 1 if t.to_i > $+.chomp('.').to_i
end
return 0
end
end
class Array
def best_version?
s = 0
for i in 1…self.length() - 1 do
t = self[i].newer? self[s]
if t == 1
s = i
elsif t == -1
return nil
end
end
return s
end
end
vv = [‘ruby-2.2.5’, ‘ruby-3.2.6’, ‘ruby-5’]
i = vv.best_version?
puts vv[i]
first, I would suggest more comments in the code at least something
above the method telling what it does. That makes it so others can see
at a glance what it is for, and rdoc can document it properly.
query methods (foo?()) generally return true/false. Also, I suspect
that comparing different packages should raise an error, not return -1.
Otherwise, I think maybe it could be condensed down a bit…
class String
# compares package version strings
def newer?(other)
this, that = split(/-/), other.split(/-/)
# check that package names match
if this[0] != that[0]
raise ArgumentError, "package names do not match"
end
# compare each section
this[1].split(".").zip( that[1].split(".") ) do |a,b|
a, b = a.to_i, b.to_i
return a > b if a != b
end
# neither one was larger:
return false
end
end
class Array
# returns the highest version string in an array
def best_version
inject{|a,b| a.newer?(b) ? b : a }
end
end
cheers,
–Mark
···
On Apr 23, 2004, at 5:17 AM, saiph wrote:
hi list,
i’m following the “learning ruby” path, i’ve found this list very
usefull the
last time (tnx), so i thought to send this simple example to better
understand what’d to be a good programming style.
expand -t 2 best_vv
class String
def newer?(s)
b = self.dup
b.sub!(/(\w±)/, ‘’)
t = $+
s.sub!(/(\w±)/, ‘’)
return -1 if t != $+ # not the same package
while (b.sub!(/(\d+.|\d+)/, ‘’)) do
t = $+.chomp(‘.’)
s.sub!(/(\d+.|\d+)/, ‘’)
return 1 if $+ == nil
return 1 if t.to_i > $+.chomp('.').to_i
end
return 0
end
end
class Array
def best_version?
s = 0
for i in 1…self.length() - 1 do
t = self[i].newer? self[s]
if t == 1
s = i
elsif t == -1
return nil
end
end
return s
end
end
vv = [‘ruby-2.2.5’, ‘ruby-3.2.6’, ‘ruby-5’]
i = vv.best_version?
puts vv[i]
i think the problem with both these approaches are that they may not reflect
the semantics of version numbers! for instance, i use the linux/libtool style
of versioning (the same used by ld.so - man ld.so)
if my code requires version 2.1.5, i could use 7.5.0 but NOT 3.0.4
-a
···
On Fri, 23 Apr 2004, Kristof Bastiaensen wrote:
On Fri, 23 Apr 2004 21:17:14 +0900, saiph wrote:
hi list,
i’m following the “learning ruby” path, i’ve found this list very usefull the
last time (tnx), so i thought to send this simple example to better
understand what’d to be a good programming style.
expand -t 2 best_vv
class String
def newer?(s)
b = self.dup
b.sub!(/(\w±)/, ‘’)
t = $+
s.sub!(/(\w±)/, ‘’)
return -1 if t != $+ # not the same package
while (b.sub!(/(\d+.|\d+)/, ‘’)) do
t = $+.chomp(‘.’)
s.sub!(/(\d+.|\d+)/, ‘’)
return 1 if $+ == nil
return 1 if t.to_i > $+.chomp('.').to_i
end
return 0
end
end
class Array
def best_version?
s = 0
for i in 1…self.length() - 1 do
t = self[i].newer? self[s]
if t == 1
s = i
elsif t == -1
return nil
end
end
return s
end
end
vv = [‘ruby-2.2.5’, ‘ruby-3.2.6’, ‘ruby-5’]
i = vv.best_version?
puts vv[i]
i think the problem with both these approaches are that they may not reflect
the semantics of version numbers! for instance, i use the linux/libtool style
of versioning (the same used by ld.so - man ld.so)
if my code requires version 2.1.5, i could use 7.5.0 but NOT 3.0.4
That was an interesting read on version number semantics, but your
example conflicts with my understanding. Wouldn’t the 2.1.5 requirement
be satisfied by 3.0.1 and in fact by 3.0.x for x>=1? The last digit,
IIRC, is a measure of backwards compatibility with previous interface
versions. So 3.0.4 isn’t even possible, because there are at most 3
previous interface versions, not 4. By the same token 7.5.0 doesn’t work
with 2.x.y thru 6.x.y because it’s “interface age” is only 0.
no. you are correct - i swapped revision with age in my example. sorry for
the confusion.
i’d really like to see something like this in the core. i use it here at work
were we have (in house joke) single test/production environment and it allows
me to upgrade libs while our near-realtime system is running because in
anything important i require an exact version. only in developemnt or
less critical code to i allow a ‘compatible’ version to be found… it’s a
real comfort to be able to upgrade libs without breaking existing code.
the method i use is:
lib/package-0.0.0.rb
lib/package.rb
where package.rb is a link to package-0.0.0.rb. the install will obviously
install both and i end up with
in site_ruby. i try to keep stuff up to date - but i don’t HAVE to.
-a
···
On Sat, 24 Apr 2004, Joel VanderWerf wrote:
Ara.T.Howard wrote:
i think the problem with both these approaches are that they may not reflect
the semantics of version numbers! for instance, i use the linux/libtool style
of versioning (the same used by ld.so - man ld.so)
if my code requires version 2.1.5, i could use 7.5.0 but NOT 3.0.4
That was an interesting read on version number semantics, but your
example conflicts with my understanding. Wouldn’t the 2.1.5 requirement
be satisfied by 3.0.1 and in fact by 3.0.x for x>=1? The last digit,
IIRC, is a measure of backwards compatibility with previous interface
versions. So 3.0.4 isn’t even possible, because there are at most 3
previous interface versions, not 4. By the same token 7.5.0 doesn’t work
with 2.x.y thru 6.x.y because it’s “interface age” is only 0.
Did I misunderstand?
–
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================