Skip to content

Instantly share code, notes, and snippets.

@rkh
rkh / head.sh
Last active January 5, 2018 16:34
# install ruby-head binary from https://rubies.travis-ci.org/
echo rvm_remote_server_url3=https://rubies.travis-ci.org > ~/.rvm/user/db
rvm get head
rvm reinstall ruby-head --binary
@glv
glv / experience_teaching_ruby_testing.md
Last active November 3, 2019 14:00
@tenderlove asked about the wisdom of teaching RSpec to new Ruby developers. I have some relevant experience. Here it is, for what it's worth.

Notes on teaching both test/unit and RSpec to new Ruby developers

@tenderlove asked "Is it good to teach RSpec (vs t/u) to people who are totally new to Ruby?" I have experience suggesting that it is a good thing; after a short back and forth, it seemed useful to write it up in detail.

Background

This goes back several years, to when I was the primary Ruby/Rails trainer for Relevance from 2006-2009. I'm guessing that worked out to probably 6-8 classes a year during those years. Since then, RSpec has changed a fair amount (with the addition of expect) and test/unit has changed radically (it has an entirely new implementation, minitest, that avoids some of the inconsistencies that made test/unit a bit confusing during the time I'm writing about here).

I started out as an RSpec skeptic. I've never been afraid of what a lot of people denigrate as "magic" in Ruby libraries … to me, if you take the trouble to understand it, that stuff's just pr

#!/usr/bin/ruby -w
$: << "../../sexp_processor/dev/lib"
$: << "lib"
require "ruby_parser"
require "sexp_processor"
require "set"
@mislav
mislav / backfill-releases.sh
Created February 5, 2014 17:37
Script to migrate releases from CHANGELOG.md to GitHub Releases
#!/bin/bash
# Usage: OAUTH_TOKEN="..." backfill-releases CHANGELOG.md [<project-title>]
set -e
log="${1?}"
project_name="${2}"
repo="$(git config remote.origin.url | grep -oE 'github\.com[/:][^/]+/[^/]+' | sed 's/\.git$//' | cut -d/ -f2-3)"
[ -n "${project_name}" ] || project_name="${repo#*/}"
Ruby 2.1.0 in Production: known bugs and patches
Last week, we upgraded the github.com rails app to ruby 2.1.0 in production.
While testing the new build for rollout, we ran into a number of bugs. Most of
these have been fixed on trunk already, but I've documented them below to help
anyone else who might be testing ruby 2.1 in production.
@naruse I think we should backport these patches to the ruby_2_1 branch and
release 2.1.1 sooner rather than later, as some of the bugs are quite critical.
I'm happy to offer any assistance I can to expedite this process.

RSpec 3 syntax is more verbose.

About twice as many characters to type to stub something:

obj.stub(client: client)                          # old
allow(obj).to receive(:client).and_return(client) # new
allow(obj).to receive(client: client)             # possible? still much longer
allow(obj, client: client)                        # I might wrap it in this
@myronmarston
myronmarston / crazy_ruby_bug.md
Last active December 14, 2015 17:38
The craziest ruby bug I've ever seen. I don't understand it. At all.

I've discovered a crazy bug that's really confusing me. I'm curious to hear if anyone can explain it.

Here's some code in foo.rb:

class Superclass
  unless ENV['NORMAL_METHOD_DEF']
    define_method :regex do
      /^(\d)$/
    end
@mislav
mislav / geoip_service.rb
Created September 3, 2012 11:48
Simple GeoIP service class using freegeoip.net and Faraday
require 'faraday_middleware'
require 'hashie/mash'
# Public: GeoIP service using freegeoip.net
#
# See https://github.com/fiorix/freegeoip#readme
#
# Examples
#
# res = GeoipService.new.call '173.194.64.19'
@mislav
mislav / easy_way.rb
Last active May 20, 2020 13:48
RESOLVE SHORT URLS before storing. Short URLs are for microblogging; you should never actually keep them around.
require 'net/http'
# WARNING do not use this; it works but is very limited
def resolve url
res = Net::HTTP.get_response URI(url)
if res.code == '301' then res['location']
else url.to_s
end
end

This is a distillation of a technique we use internally at Basho to reduce the pain of cloning some of our larger repos. It relies a bit on shell scripts and environment variables, but it can be run more directly, as I show below.

  1. Create a bare git repository in some directory, e.g. /var/lib/gitcache.

    git init --bare /var/lib/gitcache
  2. Add all repositories you want to cache as remotes to the git repository, e.g.