Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / view_source_in_clojure_repl.md
Last active August 29, 2015 14:15
[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 127.0.0.1 - nrepl://127.0.0.1:58700
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
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 / 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 / weighted_random_sampling.md
Last active April 24, 2025 04:53
[TIx 8] Weighted Random Sampling in Ruby

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 / 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 / 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