Skip to content

Instantly share code, notes, and snippets.

@O-I
O-I / scrape_mobile_site_with_ruby.md
Created March 4, 2015 19:20
[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 = 'https://github.com/O-I'

# this opens the URL and parses it as XML 
@O-I
O-I / counter_cache_has_many_through.md
Last active August 29, 2015 14:27
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
O-I / README.md
Last active December 25, 2015 22:19
Ruby Quicksort
@O-I
O-I / damm.rb
Last active August 22, 2016 01:27
Damm Algorithm
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 / profiles.clj
Last active January 8, 2017 01:11
My Clojure profile settings
{: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 / object-relational-impedance-mismatch.md
Last active November 6, 2017 23:14
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
O-I / get_character.rb
Last active November 28, 2018 18:01
Ruby Character to Unicode Converter
def get_character(hexnum)
char = ''
char << hexnum.to_i(16)
end
@O-I
O-I / univariate_polynomial_roots.rb
Last active July 31, 2019 22:26
Ruby univariate polynomial root finder
require 'matrix'
# Input: an array of the n coefficients [a_n, a_n-1,..., a_1] of
# a univariate polynomial p of degree n ([a_n]x^n + [a_n-1]x^n-1 + ... + a_1)
#
# Output: an array of all n roots of p
#
# Exploits the fact that the eigenvalues of the companion matrix of the
# monic equivalent of p are the roots of p
#
@O-I
O-I / hash_except.md
Last active August 2, 2023 17:31
[TIx 4] Remove key-value pairs from a hash and return the hash

Note: As of Ruby 3.0.0, Hash#except is now [part][1] of the language. However, Ruby does not implement Hash#except!.

Sometimes I want to remove a specific key-value pair from a Ruby hash and get the resulting hash back. If you're using Rails or ActiveSupport you can accomplish this using Hash#except:

hash = { a: 1, b: 2, c: 3 }
hash.except(:a)     # => { b: 2, c: 3 }

# note, the original hash is not modified
hash # =&gt; { a: 1, b: 2, c: 3 }