Chad Perrin: SOB

15 January 2010

the unnecessarily single-lined Ruby idiom

Filed under: Geek — apotheon @ 11:49

I keep seeing a one-line expression of a particular Ruby idiom. I think it ends up looking ugly and a bit difficult to grasp as it is usually written. I recently decided to see if it still works if done on multiple lines — and it does.

I most recently ran across it in two places. One was the Ruby Best Practices book (great book so far, by the way). The other was a Weblog post by John Nunemaker called Class and Instance Variables in Ruby. In the latter, he explains that this idiom can be used to create class instance variables which — unlike class variables — are per-class specific. By contrast, the scope of a class variable (not a class instance variable) extends across the class in which it is defined and its subclasses. Anyway, his example of creating a class instance variable with accessor methods looks like this (with an extra semicolon added by me for clarity):

class Polygon
  class << self; attr_accessor :sides; end
  @sides = 8

After playing in irb, I discovered that the fact everyone else in the world seems to want to do this in one line isn’t a result of not being able to spread it across multiple lines. Considering the one-line construction in this case looks ugly as hell to me, I’d rather have written that code like this:

class Polygon
  class << self
    attr_accessor :sides
  @sides = 8

I don’t, in fact, see any reason to cram it all into one line like that. There are times when something with a clear multiline form like this probably should be done in a single line — such as when you want to create an iterator block that only contains a single line of code:

array.each {|x| puts x }

The multiline approach would of course look like this:

array.each do |x|
  puts x

I think that, in a case like this, the single line form is clearer. There’s a distinct single line form for a block in Ruby, though. The example of the class instance variable’s accessor, on the other hand, does not have a distinct form well suited to a single line. Rather, one has to just manhandle code line terminators (the semicolons) so that Ruby won’t throw errors when cramming everything onto a single line — and it ends up looking cluttered, especially when there’s a symbol involved with its leading colon.

Am I the only person that thinks this way — that the class instance accessor is clearer in a multiline form than strung together on a single line? Is there some benefit to doing it all in a single line that I just haven’t seen?

14 January 2010

Email Composed in Notepad?

Filed under: Geek,inanity — apotheon @ 11:37

I got an email on the bugtraq list that has some weird formatting quirk infesting it. The bugtraq archive won’t show the problem for us, but I’ll copy and paste it directly from Mutt for you here:

Release Date:^M
Product: ^M
Tested Vulnerable Versions: ^M
3.1.1 and 3.1.0^M
Null Pointer^M
Hellcode Research discovered a null pointer vulnerability in Openoffice for Windows.^M
Opening a malformed ".csv" file with Openoffice, causes a crash on "soffice.bin"^M
Hellcode Research^M
The Computer Cheats (TCC)^M
Natal Networks^M

I wonder how things like this happen. It looks like someone might have composed an email in Notepad then used some Unix tool to send it to a mail user agent of some sort. This is something I have seen before, a couple of times. Is there some email client that does this crap?

If you have an explanation that escapes me at the moment, I’d love to hear it. If they’re composing emails for the bugtraq list in Notepad, though, I’m pretty sure I wouldn’t ever want to work for them.

10 January 2010

The 2009 Haul, Part 2

Filed under: Geek,Lists,Miscellaneous — apotheon @ 03:06

In Part 1, I mentioned that my two Christmases got postponed, and listed my book score from the first gathering. In the second Christmas gathering, with one person fewer attending than at the first, I got one book fewer.

Three of us do a used book exchange each year, and what we receive tends to be a surprise because unlike the rest of the books that get gifted each Christmas the selections are made by the givers without any input from the recipients. Other books tend to come from Amazon wishlists. The second gathering was where we gave each other the used book exchange gifts this year.

Without further ado, the following was my book haul for Part 2. . . .

  • Assassin’s Apprentice by Robin Hobb

    I think it was one of the SigO‘s coworkers that once told her that Robin Hobb’s books were “intelligent” fantasy. Since then, we’ve been kicking around the idea of reading something by Hobb. I guess, to make sure something actually happened with that, she decided now was a good time to give me one of Hobb’s books. This book in particular is part one of the Farseer series — a trilogy, I think. I also think this may be Hobb’s debut novel.

  • Free Culture: The Nature and Future of Creativity by Lawrence Lessig

    Lessig’s statements in favor of “free culture” have always struck me as a little utilitarian (I specifically subscribe to the anti-copyright approach served by copyfree policy, for reasons derived from free market principles), but regardless of any potential disagreement I might have with the principles that lead him to his policies — and the resulting policies — he is probably the single best-known advocate for free and open licensing outside of the narrow niche of software licensing. It’s about time I give this book a read, so I’ll have a more direct knowledge of a very influential book in the “free culture” movement (in fact, the book that probably gave the movement its name). Someone picked it out of my wishlist for me this year, and I’ll probably read it before the book Lessig endorsed that was in my Part 1 haul, Steven Levy’s Hackers.

  • Hamilton’s Curse by Thomas J. DiLorenzo

    On the front of the book, I see the words “How Jefferson’s Arch Enemy Betrayed the American Revolution — and What It Means for Americans Today”. With the promise of a thesis statement (or TL;DR summary) like that, I can’t possibly resist this book. Interestingly, there’s an endorsing quote on the cover from Ron Paul that reads “I recommend this book to my fellow Jeffersonians.” My personal take is that Jefferson was damned near perfect in principle, and tended to fold like a house of cards when faced with opposition from erstwhile allies — which, of course, in no way detracts from the value of the principles themselves. In any case, I expect to find the book a thoroughly intriguing read.

  • Heart of Darkness by Joseph Conrad

    I have not actually heard much about this book, but I know it’s a “classic”. It’s also apparently very short. This was my other used book exchange gift this year, along with Assassin’s Apprentice.

  • The Wealth of Networks: How Social Productions Transforms Markets and Freedom by Yochai Benkler

    This book’s title certainly lends to the notion that it may be a good companion volume to Wealth of Nations for “The Information Age” (do people still use that term?). Given my belief that a free (as in speech) Internet is essentially the last bastion of intellectual freedom in the world these days, regardless of how much I may agree or disagree with what this book presents, I expect to find it interesting.

Well, that’s all of it. Unless I get something wholly unexpected, that’s the end of my Christmas 2009 book haul (though the loot was actually pillaged in January 2010). I’m already looking forward to more books at the tail end of this year — but I have some great reading material to absorb my attention in the meantime.

« Newer PostsOlder Posts »

All original content Copyright Chad Perrin: Distributed under the terms of the Open Works License