More low level Ruby (XOR Hash)…
So Ruby is being a wonderful language to write in (seriously more enjoyable than most) and is giving me access (cleanly) to advanced language features I’ve wanted from other places. (Although I don’t hack in Smalltalk, Scheme or Lisp, I understand alot of features which I have desired in a more practically accessible language for a long time). Ruby seems to have slowed me down in a few places, and lower level string handling is one such place. As with the last post, I’ve been working with ASCII hex tuples and the support is a bit weaker than one might hope (this is not uncommon anyway, and as always there’s a balence of length of API vs functionality).
I’ve had to write what is effectively the reverse of the previous posting, but in the ‘guise’ of a simple xor hashing function (suitable for NMEA parsing btw ;):
[source:ruby]
def xorchecksum(str)
xor = str[0] str[1..-1].each_byte { |c| xor ^= c }
return res.chr.unpack(”H8″)[0].to_s
end
[/source]
This maybe could be more efficient somehow else, but this works. Certainly as I’m producing code that is to be production level and yet never going to be overloaded with data, this is currently suitable. I’m aware that there may be one particular efficiency increase if I try to somehow not allocate object space so often, but at this time I think it’s relatively close to that optimal already. The excess syntax required feels a bit limiting, abusing Duck Typing a little to get the correct results back.
This and reading 1959 page ETSI documentation is my current day-in day-out, but we’re getting close to release when I’ll probably be posting here a little note on what we’ve been hermitting for the last couple of months.