Untitled-1 @ 66.7% (RGB/16)

  • Archive
  • RSS
  • Ask me anything

Installing ZeroMQ Gem on OS X Snow Leopard + MacPorts

You have to specify 64-bit architecture, otherwise mkmf will try to marry it to i386.

sudo ARCHFLAGS='-arch x86_64' gem install zmq -- with-zmq-dir=/opt/local
    • #geekery
    • #zeromq
  • 3 months ago
  • Permalink
  • Share

Installing PostgreSQL on OS X Lion

Under Mac OS X Lion, the PostgreSQL one-click installer always fails on the post-install step. Ugh.

If you run this as root (or prefix every line with “sudo”, ick). This will create the “postgres” user and its home directory, initialize the database, and add PostgreSQL to system startup.

dscl . -create /Users/postgres
dscl . -create /Users/postgres NFSHomeDirectory /usr/local/postgres
dscl . -create /Users/postgres UniqueID `dscl . -list /Users UniqueID | sort -n -k +2 | tail -1 | awk '{ print $2+1 }'`
dscl . -create /Users/postgres PrimaryGroupID `dscl . -read /Users/_postgres PrimaryGroupID | awk '{ print $2 }'`
mkdir -p /usr/local/postgres/data
chown -R postgres:_postgres /usr/local/postgres
sudo -u postgres -i /Library/PostgreSQL/9.0/bin/initdb -D /usr/local/postgres/data
curl -Lo /Library/LaunchDaemons/org.postgresql.postgres.plist http://be.gs/postgresql.plist
launchctl load /Library/LaunchDaemons/org.postgresql.postgres.plist

Why not use the default “_postgres” user? I don’t what that user account is used for, or what Apple plans to do with that account in the future.

    • #geekery
    • #postgresql
  • 8 months ago
  • 12
  • Permalink
  • Share

Headers only with Curl on GETs

Sometimes I want to see HTTP headers only.

If I use Curl (curl -I), and that does a HEAD statement. Problem is not everyone respects the difference between GET and HEAD, so you end up with results that can be wildly different — 200s with full content, 400s, 403s, and sometimes, “easter eggs” (when you’re troubleshooting something, finding a rotten easter egg IS NOT CUTE).

Here’s an alias to restore sanity.

Add the following to ~/.bashrc:

alias hdrs='curl -so /dev/null -D-'
    • #geekery
  • 1 year ago
  • Permalink
  • Share

Ruby 102: One-Line Rescue

I’ve gotten used to the try { } catch { } blocks of Java to handle exceptions.

In Ruby, it’s begin/rescue/end.


So, I started off this morning with:

def load_servers
  begin
    return false unless fp = File.open('servers.db', 'r')
    @@servers.push fp.readline
  rescue EOFError
  rescue
    fp.close unless !fp
    return false
  end
end


I later find out that, File.open().each will handle EOFError for me, so I went to:

def load_servers
  begin
    File.open('servers.db', 'r').each do |line|
    @@servers.push line
  end
  rescue
    return false
  end

  return true
end


I conferred with my friend, Jason, who said I should do it in one line.

def load_servers
  begin
    @@servers = File.open('servers.db', 'r').read.split(/\n/)
  rescue
    return false
  end

  return true
end


After showing him, he screams at me “ONE LINE MAN!”, and shows me how to do a one-line rescue, and instantly my mind was blown.

def load_servers
  @@servers = File.open('servers.db', 'r').read.split(/\n/) rescue false
  return true
end


Then we tidy up with best practices: you should only ever return one class (type) — Object or nil.

def load_servers
  @@servers = File.open('servers.db', 'r').read.split(/\n/) rescue nil
  @@servers
end


EDIT: Kelly tells me that, the last line isn’t needed as the value being assigned is implicitly returned if it’s the last line in a function. Jason adds that implicit returns are ‘shady’ (a.k.a. hard to read, if you’re just glancing very quickly at things).

def load_servers
  return @@servers = File.open('servers.db', 'r').read.split(/\n/) rescue nil
end


And that, is my morning in Ruby.

    • #geekery
    • #ruby
  • 1 year ago
  • 1
  • Permalink
  • Share
← Newer • Older →
Page 1 of 3

About

Me. Manhattan. Twenty-Something.

This is the chronologically-sorted collection of thoughts from my otherwise unorganized mind.

flickr | ogali
twitter | oogali

Twitter

loading tweets…

Following

  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr