Skip to main content

Benjamin Oakes

Photo of Ben Oakes

Hi, I'm Ben Oakes and this is my geek blog. Currently, I'm a Ruby/JavaScript Developer at Liaison. Previously, I was a Developer at Continuity and Hedgeye, a Research Assistant in the Early Social Cognition Lab at Yale University and a student at the University of Iowa. I also organize TechCorridor.io, ICRuby, OpenHack Iowa City, and previously organized NewHaven.rb. I have an amazing wife named Danielle Oakes.

Filtering for the Today I Learned category. Clear

The poison arrow frog’s toxin has an anti-toxic evil twin

by Ben

tmp_10313-schrecklicherpfeilgiftfrosch-01-800x611-1090014168

A 24 step (!) synthesis yielded two milligrams of batrachotoxin, or two frogs’ worth. If it can be scaled up, then collection in the wild will become irrelevant. But the synthesis also revealed some interesting biology. That’s because, like many chemical syntheses, the protocol generated not only batrachotoxin, (-)-BTX, but also its evil twin, (+)-BTX.

Many carbon-based molecules—i.e. most biologically relevant molecules—have a handedness, determined by the way the various other atoms they contain are arranged around the carbon. Molecules can have the same constituent atoms but be mirror images of each other, just like your right and left hands. They are the same but are not superimposable one atop the other. This seemingly small difference matters. All life on Earth, for some as yet undiscovered evolutionary reason, uses exclusively left-handed amino acids to make proteins and right-handed nucleic acids for their genetic material.

Source: The poison arrow frog’s toxin has an anti-toxic evil twin | Ars Technica

I wouldn’t exactly call the result “anti toxic,” but I learned something about chemistry from this article… (I’m pretty sure I learned about the mechanics of the neurotoxin in a neuroscience class.)

Ruby Simple HTTP Server, minimalist Rake

by Ben

I use a really simple HTTP server all the time. It happens to be written in Python:

python -m SimpleHTTPServer 5000

That serves all the files in the current directory over HTTP on port 5000. Honestly, it works just fine, but I’ve always wondered if Ruby had an equivalent.

Here it is:

ruby -run -e httpd . -p 5000

(from Aaron Patterson’s tweet found via Zach Morek)

It’s pretty much the same, except it’s written in Ruby. More often than not, that’s not a big difference — except I can understand the code behind it.

#
# = un.rb
#
# Copyright (c) 2003 WATANABE Hirofumi <eban@ruby-lang.org>
#
# This program is free software.
# You can distribute/modify this program under the same terms of Ruby.

# [...]

##
# Run WEBrick HTTP server.
#
# ruby -run -e httpd -- [OPTION] DocumentRoot
#
# --bind-address=ADDR address to bind
# --port=NUM listening port number
# --max-clients=MAX max number of simultaneous clients
# --temp-dir=DIR temporary directory
# --do-not-reverse-lookup disable reverse lookup
# --request-timeout=SECOND request timeout in seconds
# --http-version=VERSION HTTP version
# -v verbose
#

def httpd
  setup("", "BindAddress=ADDR", "Port=PORT", "MaxClients=NUM", "TempDir=DIR",
        "DoNotReverseLookup", "RequestTimeout=SECOND", "HTTPVersion=VERSION") do
    |argv, options|
    require 'webrick'
    opt = options[:RequestTimeout] and options[:RequestTimeout] = opt.to_i
    [:Port, :MaxClients].each do |name|
      opt = options[name] and (options[name] = Integer(opt)) rescue nil
    end
    unless argv.size == 1
      raise ArgumentError, "DocumentRoot is mandatory"
    end
    options[:DocumentRoot] = argv.shift
    s = WEBrick::HTTPServer.new(options)
    shut = proc {s.shutdown}
    siglist = %w"TERM QUIT"
    siglist.concat(%w"HUP INT") if STDIN.tty?
    siglist &= Signal.list.keys
    siglist.each do |sig|
      Signal.trap(sig, shut)
    end
    s.start
  end
end

So how does it work? It’s actually a little surprising. Here’s the command again for reference:

ruby -run -e httpd . -p 5000

In order:

While that code is probably too clever, it’s nice to have a simple HTTP server wherever I have Ruby.

Even more, the concept is reusable:

# File: ake.rb
# Minimalist rake.  :)
def greet
  puts "Hello, #{ ARGV[0] }!"
end

Here’s the output

$ ruby -I . -rake -e greet Ben
Hello, Ben!
$ ruby -r ./ake -e greet Ben
Hello, Ben!

That could be a nice minimalist way to write some helper scripts without Rake or Thor.

Disabling RdRand in Linux

by Ben

Linus Responds To RdRand Petition With Scorn – Slashdot.
Torvalds’ response to whether RdRand could be compromised in the Linux kernel | Hacker News.
Linus Torvalds responds – Change.org.

This got a surprising amount of attention, mostly because of one of Linus’ classic responses. My feeling is that it’s good to question these types of things. However, making a petition against using a hardware random number generator, and including some vague concerns about the NSA probably isn’t quite the right way to go about it.

Anyway, I learned today (from a Slashdot comment) that you can simply pass nordrand to the kernel to disable RdRand if you really don’t like it. Whether or not “the NSA” is on your list of reasons is up to you.

Today I Learned: You can unlink a file and still use it

by Ben

Unlink after creation

On POSIX systems, it’s possible to unlink a file right after creating it, and before closing it. This removes the filesystem entry without closing the file handle, so it ensures that only the processes that already had the file handle open can access the file’s contents. It’s strongly recommended that you do this if you do not want any other processes to be able to read from or write to the Tempfile, and you do not need to know the Tempfile’s filename either.

From Ruby’s Tempfile documentation. In Ruby/POSIX parlance, unlink means delete, so this was surprising to me.