Skip to content

Instantly share code, notes, and snippets.

@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 / README.md
Last active December 25, 2015 22:19
Ruby Quicksort
@O-I
O-I / all_about_nil.md
Last active August 29, 2015 13:57
Master of nil
@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 / fizzbuzz.hs
Created July 11, 2014 16:35
FizzBuzz in Haskell
fizzbuzz :: Int -> [String]
fizzbuzz n = take n $ addNums $ zipWith (++) fizz buzz
where fizz = cycle ["", "", "Fizz"]
buzz = cycle ["", "", "", "", "Buzz"]
addNums = zipWith numOrNot $ map show [1..]
numOrNot = \ x y -> if null y then x else y
@O-I
O-I / random_sum.rb
Created July 11, 2014 16:44
Generate n unique random natural numbers whose sum is m
# Question source: http://codegolf.stackexchange.com/q/8574/12268
# Write an algorithm in any programming language you desire
# that generates n unique randomly-distributed random natural
# numbers (i.e. positive integers, no zero), sum of which is
# equal to t, where t is bigger than or equal to n*(n+1)/2.
# Example: Generate 10 unique random natural numbers, sum of which is equal to 500.
def rand_sum(size, sum)
@O-I
O-I / guardian_api_test.rb
Last active August 29, 2015 14:04
Experimenting with the Guardian Content API
require 'open-uri'
require 'dotenv'
require 'json'
require 'pry'
Dotenv.load
# Basic structure of a Guardian API request:
# BASE_URI + endpoint + queries + api-key + params
@O-I
O-I / nsect.rb
Created September 24, 2014 17:26
Nsect an array in Ruby
# 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]
@O-I
O-I / ulam_spiral.rb
Created October 2, 2014 21:03
Ulam spiral
require 'prime'
def ulam_spiral(n)
matrix = Array.new(n) { Array.new(n) }
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
O-I / define_method.md
Last active August 29, 2015 14:12
[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)