Skip to content

Instantly share code, notes, and snippets.

O-I /
Last active Nov 6, 2017
Object-Relational Impedance Mismatch

The fundamental difficulty

Straight from the [wiki]:

Fundamentally, objects (instances) reference one another and therefore form a graph in the mathematical sense (a network including loops and cycles). Relational schemas are, in contrast, tabular and based on the relational algebra, which defines linked heterogeneous tuples (groupings of data fields into a "row" with different types for each field).

Converting linked tabular rows to graph structures is hard, and even described as the [Vietnam of Computer Science][vietnam-of-cs].

Further reading:

O-I /
Last active Jul 19, 2021
The Four Elements of Simple Design

The Four Elements of Simple Design

  • Introduced by [Kent Beck][beck] in the 1990s.
  • Part of his software development methodology [Extreme Programming][extreme-programming].
  • His exact wording appears in the [White Book][white-book].

The rules can be stated as followed:

  1. Passes all tests
  2. Maximizes clarity
O-I /
Last active Jul 13, 2021
Blockchain links
O-I / damm.rb
Last active Aug 22, 2016
Damm Algorithm
View damm.rb
module Damm
# See
def lookup number
number.to_s.each_char.inject(0) do |m,v|
O-I /
Last active Aug 29, 2015
Counter caching on a has_many :through association in Rails 3.2

I'm trying to figure out if using counter_cache on a has_many :through association is still considered kosher in Rails 3.2. I've read several posts that distill the steps for setting this up (e.g., see 1 and 2). On the other hand, it seems like counter_cache on has_many :through was an unintended side effect that was never a supported feature of Rails and was ultimately removed.

Here's my specific scenario:

# Event model
has_many :event_attendees, dependent: :destroy
has_many :attendees, through: :event_attendees, source: :user

# EventAttendee model
O-I /
Last active Jul 28, 2021
[TIx 8] Weighted Random Sampling in Ruby

One of the many reasons I love working with Ruby is it has a rich vocabulary that allows you to accomplish your goals with a minimal amount of code. If there isn't a method that does exactly what you want, it's usually possible to build an elegant solution yourself.

Let's take the example of simulating the rolling of a die.

We can represent a die as an array of its faces.

die = [*?⚀..?⚅]
# => ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]
O-I / profiles.clj
Last active Jan 8, 2017
My Clojure profile settings
View profiles.clj
{:user {:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/tools.namespace "0.2.10"]
[spyscope "0.1.5"]
[criterium "0.4.3"]]
:injections [(require '( repl find))
; try/catch to workaround an issue where `lein repl`
; outside a project dir will not load reader literal
; definitions correctly:
(try (require 'spyscope.core)
(catch RuntimeException e))]
O-I /
Created Mar 4, 2015
[TIx 7] Scraping the mobile version of a site with Ruby

I found myself in a situation where I wanted to examine the layout of the mobile version of a particular website. I tend to use Ruby's OpenURI module and the Nokogiri gem for my webscraping needs, and it turns out it's really easy to get a mobile version of the site with a bit more effort:

require 'open-uri'
require 'nokogiri'

# let's look at my GitHub profile as an example
url = ''

# this opens the URL and parses it as XML 
O-I /
Last active Aug 29, 2015
[TIx 6] View source code in a Clojure REPL

This is one of those things that was right in front of my face, but I figure it's worth a mention. I was trying to figure out the best way to view the source code for Clojure's frequencies function — ideally while in a REPL. I found this older post mentioning a source function in the clojure.contrib.repl-utils namespace. I launched a REPL to see if I could use it and, well, here's what I saw:

$ lein repl
nREPL server started on port 58700 on host - nrepl://
REPL-y 0.3.5, nREPL 0.2.6
Clojure 1.6.0
Java HotSpot(TM) 64-Bit Server VM 1.7.0_45-b18
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")