public
Created — forked from rklemme/hexnum-simple.rb

  • Download Gist
hexnum.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
class HexNum < Numeric
 
# Create a new instance from an int or String.
def initialize(val)
case val
when String
@i = parse_string(val)
@s = val.frozen? ? val : val.dup.freeze
when Numeric
@i = val.to_i
else
raise ArgumentError, 'Cannot convert %p' % val
end
end
 
# conversions
 
def to_s
@s ||= (@i < 0 ? '-0x%x' % -@i : '0x%x' % @i).freeze
end
 
def to_i
@i
end
 
alias to_int to_i
 
def to_hex
self
end
 
# equivalence
 
def eql?(num)
self.class.equal?(num.class) && @i == num.to_i
end
 
alias == eql?
 
# hash code calculation
 
def hash
@i.hash
end
 
# various tests that Fixnum also has
 
def zero?
@i.zero?
end
 
def nonzero?
@i.nonzero?
end
 
def odd?
@i.odd?
end
 
def even?
@i.even?
end
 
def between?(a, b)
@i.between? a, b
end
 
def integer?
true
end
 
def real?
true
end
 
# coercion
 
def coerce(o)
[HexNum.new(o.to_int), self]
end
 
# comparability
 
include Comparable
 
def <=>(o)
case o
when HexNum
@i <=> o.to_i
when Numeric
@i <=> o
else
a, b = o.coerce(self)
a <=> b
end rescue nil
end
 
# unary operators
 
def +@
self
end
 
def -@
HexNum.new(-@i)
end
 
# binary operators
 
def +(o)
case o
when Integer
HexNum.new(@i + o)
when Numeric
HexNum.new(@i + o.to_i)
else
a, b = o.coerce(self)
a + b
end
end
 
def -(o)
case o
when Integer
HexNum.new(@i - o)
when Numeric
HexNum.new(@i - o.to_i)
else
a, b = o.coerce(self)
a - b
end
end
 
def *(o)
case o
when HexNum
HexNum.new(@i * o.to_i)
when Numeric
HexNum.new(@i * o)
else
a, b = o.coerce(self)
a * b
end
end
 
def /(o)
case o
when HexNum
HexNum.new(@i / o.to_i)
when Numeric
HexNum.new(@i / o)
else
a, b = o.coerce(self)
a / b
end
end
 
# asymmetric binary operators
 
def **(o)
case o
when HexNum
HexNum.new(@i ** o.to_i)
when Numeric
HexNum.new(@i ** o)
else
a, b = o.coerce(self)
a ** b
end
end
 
def <<(o)
HexNum.new(@i << o.to_int)
end
 
def >>(o)
HexNum.new(@i >> o.to_int)
end
 
# bit operators
 
def &(o)
HexNum.new(@i & o.to_int)
end
 
def |(o)
HexNum.new(@i | o.to_int)
end
 
# freeze
 
def freeze
to_s
super
end
 
private
 
# Parse a string with valid format (0x1234).
def parse_string(str)
if %r{\A[-+]?(?:0x)?[a-h0-9]+\z}i =~ str
str.to_i(16)
else
raise ArgumentError, 'Cannot parse %p' % str
end
end
end
 
# more conversions
 
def HexNum(i)
HexNum.new(Integer(i))
end
 
class Object
def to_hex
HexNum.new(to_i)
end
end
 
# ===== Test Code ===================================================
 
h1 = HexNum.new 83
h2 = 83.to_hex
 
printf "%-30s %p\n", 'h1 = HexNum.new 83', h1
printf "%-30s %p\n", 'h1.to_i', h1.to_i
printf "%-30s %p\n", 'h2 = 83.to_hex', h2
printf "%-30s %p\n", 'h2.to_int', h2.to_int
printf "%-30s %p\n", 'h1 == h2', h1 == h2
printf "%-30s %p\n", 'h2.eql? h1', h2.eql?(h1)
# printf "%-30s %p\n", 'h1.odd?', h1.odd?
printf "%-30s %p\n", 'h1 <=> h2', h1 <=> h2
 
h3 = h1 + 17
h4 = 17 + h2
 
printf "%-30s %p\n", 'h3 = h1 + 17', h3
printf "%-30s %p\n", 'h4 = 17 + h1', h4
printf "%-30s %p\n", 'h1 < h3', h1 < h3
printf "%-30s %p\n", 'h2 >= h4', h2 >= h4
 
h5 = -h1
 
printf "%-30s %p\n", 'negated', h5
printf "%-30s %p\n", 'neg sum', h5 + h2
 
printf "%-30s %p\n", 'h1 / 1.2', h1 / 1.2
printf "%-30s %p\n", 'h3 / h1', h3 / h1
 
ha = (1..10).map { HexNum(rand(100)) }
printf "%-30s %p\n", 'random array', ha.map {|h| h.to_s}
printf "%-30s %p\n", 'sorted', ha.sort.map {|h| h.to_s}
 
[80, 83, 84, 80.1, 83.0, 84.3, 1 << 40, -(1 << 40)].each do |n|
printf "%20s %-10p h1<=>n %2d n<=>h1 %2d\n", n, n.class, h1 <=> n, n <=> h1
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.