Secure random strings in Ruby

File this under “Well-documented features I never noticed before.” In Ruby, if you need to generate random strings for various purposes, there’s a native module: SecureRandom.

In Ruby 1.8, you can generate Base-64-encoded strings, hex, random numbers (integers), and random bytes. Ruby 1.9 adds the ability to generate UUIDs and URL-safe Base64 strings. Really simple, and not particularly interesting — unless, like me, you found yourself thinking you had to write this functionality yourself.

Facts about K-Mart

Don’t ask why I was spending my Friday night this way, but I was just reading about Juno, the ISP that I assumed went out of business a decade ago. It turns out that they merged with NetZero (another ISP I forgot all about), and formed United Online. (The interesting thing about their merger is that they were in a ferocious legal battle, and somehow decided that, rather than continuing to sue each other, they were better of merging into one company. That seems like the least likely outcome.) Wikipedia mentioned that United Online also holds a third Internet service — Kmart’s BlueLight ISP. This “WTF” sentence led to me reading a lot about Kmart.

So first things first, Kmart is still in business. I was surprised to learn this. I asked my roommate and he was too. We both just kind of assumed that, like Juno and NetZero, they had gone out of business when their names faded from our memory years ago. It turns out that Kmart has about 1,300 stores in the United States.

I glossed over the company’s history, but it’s interesting to note that the company was started by Sebastian Kresge — then working as a traveling salesman selling to Woolworth’s — in 1897. In 1912 he incorporated S.S. Kresge Corporation. In 1918, the corporation was listed on the NYSE. In 1924, Kresge’s personal net worth was estimated at $325 million. (In 1924 dollars.) It wasn’t until 1977 that S.S. Kresge Corporation was renamed to Kmart.

But back to random facts about Kmart. It turns out that, between 2004 and 2005, Kmart bought Sears. I apparently failed to notice this at any point in the past 6 years. The purchase was a mere $11 billion. The name was changed to Sears Holdings Corporation. (So it’s interesting to note that Kmart bought Sears, but Kmart is now a brand of “Sears Holdings Corporation,” which gives a confusing sense of who bought who.)

It also turns out that Sears Holding Corporation owns a lot more than I was aware of.

For example, Kmart bought Waldenbooks in 1984, and in 1994, Borders and Waldenbooks merged.

The Kmart-OfficeMax lineage hurts my head, but Kmart more or less bought OfficeMax in 1990 before taking it public in 1995 and then selling off its remaining shares.

Also in 1990, Kmart bought Sports Authority. Like with OfficeMax, Sports Authority was spun off from Kmart proper in 1995.

And as for Kmart’s ISP? It, too, has a confusing history. They started it as a dialup ISP in 1999, as a free service supported by in-browser ads. The company was later spun off from Kmart. In 2001, after becoming a paid service, Kmart bought BlueLight back. In 2002, Kmart filed for bankruptcy, and that’s when United Online stepped in to purchase BlueLight. (It seems that Kmart filed for bankruptcy protection in 2002. It emerged from bankruptcy in 2003, and in 2004 it purchased Sears.)

Okay, so maybe I’m a little weird… But I found this moderately fascinating.

IMAP Search Musings

I think most geeks get a ton of email. I’ve been rather selective in what lists I join (and what mail I just auto-delete at the server level), so I only get perhaps a couple hundred emails a day now.

For various ridiculous reasons, searching a mailbox is hard. Most of my mail clients will do it, but sometimes I really want to do it myself because I want to be more exacting. Lately I’ve been tinkering more and more with using IMAP from the command line. Partially it’s that IMAP is really just a little bit unusual and kind of intriguing. But mostly, it’s just that, from time to time, it’s easier for me to just send some manual IMAP commands if I know what I’m doing. (For example, I have absolutely no idea how to subscribe to a particular folder in any of the mail clients I have available. I could wade through the menus and maybe find it, but it’s easier for me to just use Net::IMAP to subscribe to the folder.)

Like most implementations, Ruby’s Net::IMAP library provides a search method. And, like most implementations, it does a very poor job of documenting what is supported. At the risk of sounding like I’ve lost my mind, I actually found the IMAP RFC (3501) to be the easiest bit of documentation to understand. Below are some examples:

You can chain keys, so the following is a valid search command:

FROM "matt" SUBJECT "bacon"

But what isn’t abundantly clear is that search terms are combined with a logical AND, so the above will only match mail from Matt with “bacon” in the subject line.

This appears to be the way to get a logical OR instead:

(FROM "matt") OR (SUBJECT "bacon")

After spending a while trying to get all possible keys for a text search — e.g., (FROM "foo") OR (SUBJECT "foo") OR (CC "foo")... — I realized that there’s an easier way: the TEXT key, which searches headers and the body. So for my generic search method, the search simply became TEXT "foo".

You may also find some of the other keys interesting, like the ability to search for RECENT or FLAGGED. I don’t intend to provide an exhaustive list here, however.