Some code:
class Awk < String
include Comparable
@@intpattern = /^\s*[-+]?\d+$/
@@casesensitive = false
def self.casesensitive(val)
@@casesensitive = val
end
def initialize(str)
super(str.to_s)
end
def <=>(other)
if @@intpattern =~ self && @@intpattern =~ other
to_i <=> other.to_i
else
if @@casesensitive
super
else
# What to use here ?
String.new(downcase) <=> String.new(other.downcase)
end
end
end
def ==(other)
(self <=> other) == 0
end
end
a = Awk.new('a')
b = Awk.new('A')
if a == b
puts 'equal'
end
a = Awk.new('21')
b = Awk.new('3')
if a > b
puts "OK"
end
The problem I'm having is in the case insensitive case: it's somehow
asymmetrical: while to_i return an integer, to_s does _not_ return a
String but an Awk (as does downcase).
This means I can't use
downcase <=> other.downcase
as I's like to. Is it possible to use some variation of super ?
The above construct works, but strikes me as expensive and clumsy.
Han Holl