4 ms·
How to write a cell address encoder in ruby
- DarkWiiPlayer 8y agoA class that has 2 methods and no state whatsoever. This is exactly why I don't like much of the ruby code out there. EDIT: Correct me if I'm wrong, but spreadsheet column addresses are basically just base-26 encoded numbers, where the digits are 'A'...'Z'. If you convert them into their base-2 representation, you could store 65536 rows, instead of the 26*26=676 you can store with two integers. An additional consideration would be that spreadsheets are often of fixed width and grow downwards, so you can use more bits for indexing rows than columns. This depends on the use-case though.
- amiralles 8y agoYou are right about classes with no state. Nonsense. In fact, if you take a look at the gist I linked to the post (https://bit.ly/2NgWBJK https://bit.ly/2NgWBJK), you'll there is no class, nor module, nor anything like that, just a couple of ruby methods on a file. Ruby can do that, too ;)) I'm not sure on how actual spreadsheets encode cell addresses. In my particular case, what I had to do was to find a way to (efficiently) encode any cell address in the range [A1..ZZ(10^6)] into a 64 bits integer. I needed 64 bits integers because that's what I use as a key to store/retrieve cells for the in-memory data store. (An avltree indexed by integer keys.) As I mentioned in the post, the real code was written in C and targeted 64 bits platforms. Maybe that's way I doesn't make sense under OOP lenses on 32 bits systems. Maybe I'll do a follow up post on "how to store spreadsheets cells on avltrees" to show why this encoding mechanism may make sense... Thanks for you comments. Let's see how it goes!