Skip to content

Instantly share code, notes, and snippets.

O-I

Block or report user

Report or block O-I

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@O-I
O-I / object-relational-impedance-mismatch.md
Last active Nov 6, 2017
Object-Relational Impedance Mismatch
View object-relational-impedance-mismatch.md

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
O-I / four_elements_simple_design.md
Last active Mar 18, 2019
The Four Elements of Simple Design
View four_elements_simple_design.md

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
O-I / blockchain-links.md
Last active Feb 15, 2018
Blockchain links
View blockchain-links.md
@O-I
O-I / damm.rb
Last active Aug 22, 2016
Damm Algorithm
View damm.rb
module Damm
# See http://en.wikipedia.org/wiki/Damm_algorithm
TABLE=["0317598642","7092154863","4206871359","1750983426","6123045978",
"3674209581","5869720134","8945362017","9438617205","2581436790"]
def lookup number
number.to_s.each_char.inject(0) do |m,v|
TABLE[m][v.to_i].to_i
end
end
@O-I
O-I / counter_cache_has_many_through.md
Last active Aug 29, 2015
Counter caching on a has_many :through association in Rails 3.2
View counter_cache_has_many_through.md

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
O-I / weighted_random_sampling.md
Last active Nov 25, 2019
[TIx 8] Weighted Random Sampling in Ruby
View weighted_random_sampling.md

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
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 '(clojure.tools.namespace 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
O-I / scrape_mobile_site_with_ruby.md
Created Mar 4, 2015
[TIx 7] Scraping the mobile version of a site with Ruby
View scrape_mobile_site_with_ruby.md

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 = 'https://github.com/O-I'

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

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 127.0.0.1 - nrepl://127.0.0.1:58700
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")
You can’t perform that action at this time.