Skip to content

Instantly share code, notes, and snippets.

@O-I
O-I / git_branch_dash_m.md
Last active August 29, 2015 14:14
[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
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 # => { a: 1, b: 2, c: 3 }
@O-I
O-I / extract_uri.md
Last active August 19, 2023 01:02
[TIx 3] Extracting links from text with URI::extract

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 / exclude_tests_from_gem.md
Last active August 29, 2015 14:13
[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
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)
@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 / 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 / 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 / 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 / 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