Rewriting Ruby’s #pack and #unpack Methods in Crystal

In Ruby, especially while writing low-level protocols, you may have encountered Array#pack() and/or String#unpack(). If you’ve ever experimented with them, they may seem mysterious; they take a cryptic string as a parameter (maybe something like C4 or w*) and seem to return gibberish (or, conversely, convert gibberish into something humans can understand). I’m not going to go into tons of detail, nor am I going to cover every format that these methods accept, but I’ll cover a few that I’ve used.

What led me to writing this post was actually some recent experimenting that I’ve been doing with Crystal. I’m trying my hand at writing a simple BER parser/encoder as a part of my journey to writing an LDAP library for Crystal. The lack of an LDAP library is honestly the biggest reason I haven’t used Crystal for more things. Since BER is a binary method of encoding, the Ruby LDAP BER code uses a ton of Array#pack() and String#unpack(). Unfortunately, Crystal doesn’t have analogous methods for its Array class or String struct so I’ve had to write my own.

Here, I’ll describe a few of the formats supported by #pack and write some compatible examples in Crystal.

Continue reading Rewriting Ruby’s #pack and #unpack Methods in Crystal

Quadratic Confusion

I have a horrible memory. I don’t mean just that I misplace things or forget names; it takes a lot of effort to commit arbitrary facts, figures, dates, etc., to my long-term memory. So throughout my school years, most of my studying was for things like History, trying hard to remember dates and statistics that I would quickly eject from my mind after my next exam. I seldom had to study for Math or Science though, because I figured out something that worked for me there: learning how and why things work rather than just memorizing formulas. This worked well for those subjects, but I do remember stumbling in algebra when I was not able to factor quadratic functions. There was a handy Swiss army knife of sorts for this, of course, in the Quadratic Formula.

I avoided this formula as much as possible, usually by spending way too much time trying to guess the factors myself, or by converting from “standard form” to “vertex form”, or guessing, or skipping that question. This was almost entirely because I could not bring myself to memorize the formula. Call it laziness, or foolishness, or whatever you’d like.

Well, recently I decided to brush up on my math skills. After yet again encountering the need for this equation, I decided enough is enough. Since I can’t memorize the equation, I will instead learn where it comes from by deriving it from the standard form of a quadratic function. This is my attempt to do so.
Continue reading Quadratic Confusion