Skip to content

Instantly share code, notes, and snippets.

@O-I
O-I / weighted_random_sampling.md
Last active Sep 12, 2021
[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 / four_elements_simple_design.md
Last active Jul 19, 2021
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 Jul 13, 2021
Blockchain links
View blockchain-links.md
@O-I
O-I / hash_except.md
Last active Jan 1, 2021
[TIx 4] Remove key-value pairs from a hash and return the hash
View hash_except.md

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                # => { a: 1, b: 2, c: 3 }
@O-I
O-I / extract_uri.md
Last active Aug 14, 2020
[TIx 3] Extracting links from text with URI::extract
View extract_uri.md

I have a simple Rails app that collects all the tweets I favorite on Twitter so I can sort and search through them at my leisure. Many of those favorites contain links I'd like to refer to, so I wrote a helper method that converts them to clickable anchor tags that looked like this:

# app/helpers/favorites_helper.rb
module FavoritesHelper

  # snip
  
  def text_to_true_link(tweet_text)
    urls = tweet_text.scan(/https*:\/\/t.co\/\w+/)
@O-I
O-I / univariate_polynomial_roots.rb
Last active Jul 31, 2019
Ruby univariate polynomial root finder
View univariate_polynomial_roots.rb
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 / get_character.rb
Last active Nov 28, 2018
Ruby Character to Unicode Converter
View get_character.rb
def get_character(hexnum)
char = ''
char << hexnum.to_i(16)
end
@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 / 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))]