Skip to content

Instantly share code, notes, and snippets.

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 Dec 25, 2015
Ruby Quicksort
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 /
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")
O-I /
Last active Aug 29, 2015
[TIx 5] Rename an unpushed local git branch

Here's a scenario I found myself in recently. I was looking to fix a small bug in an open source project I admire. I forked the repo, checked out a feature branch, and got to work getting myself lost in the land of Minitest.

I have a habit of making small, atomic commits so I tend to run git status frequently. On doing so, I noticed something like this:

$ git status
On branch fix-tpyo
Changes not staged for commit:
  # snip
O-I /
Last active Aug 29, 2015
[TIx 2] Don't package test files with your Ruby gem

Here's something to think about the next time you're twiddling your thumbs waiting for a bundle install or gem push command to finish running. Why did that take so long? Often we think of issues of speed solely in terms of the quality of our network connection and pay less attention to actual file sizes. I don't think anyone will argue, though, that given the same network speed, a 10MB file takes longer to download than a 100KB one. And if both did the exact same thing and you needed to download it and dozens of cousins like it, say, several times per week, wouldn't you prefer smaller?

Most of the gems we use on a daily basis weigh in at 2 - 36 times more than they need to. That's because, by default, they include a very important set of files for development that are unnecessary dead weight in a packaged gem — your tests. This issue filed by @sferik on the bundler gem does an excellent job of summing this up.

Fortunately, th

O-I /
Last active Aug 29, 2015
[TIx 1] Module#define_method behavior across Ruby versions

Here's today's scenario. I'm working on mocking requests to an API to test a Ruby wrapper I've built for it. I have a spec_helper file with several convenience methods that look something like this:

# spec_helper.rb
# snip

def stub_get(path, options = {})
  endpoint = DEFAULT_API_URL + path
  headers  = DEFAULT_HEADERS
  stub_request(:get, endpoint)
View ulam_spiral.rb
require 'prime'
def ulam_spiral(n)
matrix = { }
path = [*1..n*n].reverse
padding = (n*n).to_s.size
layer = 0
until path.empty?
matrix[layer].map! { |l| l || path.shift }
matrix = matrix.transpose.reverse
O-I / nsect.rb
Created Sep 24, 2014
Nsect an array in Ruby
View nsect.rb
# nsect takes an array and optional positive integer n (default is 3)
# and returns the array partitioned into n arrays, (n-1) of which are
# of size i, the nth partition being of size (i-1), i, or (i+1).
# Examples below:
def nsect(arr, n = 3)
i = (arr.size + 1) / n
ans = []
(n-1).times do |j|
ans << arr[(0+j)*i...(1+j)*i]