Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
michaelfeathers / zom.rb
Last active September 18, 2017 22:12
Find the number of calls in Ruby code that have happened no times, just once, or many times.
require 'set'
require 'find'
class Array
def to_h; Hash[self]; end
def to_set; Set.new(self); end
def freq; group_by {|e| e }.map {|k,v| [k,v.count] }.to_h; end
end
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'rom', github: 'rom-rb/rom'
gem 'rom-sql', github: 'rom-rb/rom-sql'
gem 'rom-repository', github: 'rom-rb/rom-repository'
gem 'sqlite3'
end
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'rom', github: 'rom-rb/rom'
gem 'rom-sql', github: 'rom-rb/rom-sql'
gem 'rom-repository', github: 'rom-rb/rom-repository'
gem 'sqlite3'
end
@O-I
O-I / weighted_random_sampling.md
Last active February 21, 2024 19:02
[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 = [*?⚀..?⚅]
# => ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]
@aflatter
aflatter / password.rb
Created March 12, 2015 14:37
SCrypt::Password attribute type for Virtus
require 'scrypt'
module Virtus
class Attribute
class Password < ::Virtus::Attribute
primitive SCrypt::Password
def coerce(value)
value ? SCrypt::Password.new(value) : nil
rescue SCrypt::Errors::InvalidHash
@solnic
solnic / gist:9b9deedd7891e2c7ac83
Last active August 19, 2016 08:46
Virtus pg array type for Sequel
module Virtus
class PGArray < Attribute
primitive Sequel::Postgres::PGArray
def primitive
options[:primitive]
end
def coerce(value)
Sequel.pg_array(value)
@glv
glv / trouble-free-bundler.md
Last active March 16, 2018 14:50
This is a thing I wrote over a year ago for the internal LivingSocial wiki. There's nothing LS-specific about it, and it seems generally useful, so …

Trouble-Free Bundler

Every couple of weeks, I hear someone complaining about some difficulties with Bundler. Yesterday, it happened twice. But somehow I just never have those difficulties. I'm not saying Bundler is perfect; certainly in its early days it wasn't even close. But for the past two years it's been incredibly solid and trouble-free for me, and I think a large part of the reason is the way I use it. Bundler arguably does too much, and just as with Git, a big part of it is knowing what not to do, and configuring things to avoid the trouble spots.

@gnarf
gnarf / ..git-pr.md
Last active April 12, 2024 22:00
git pr - Global .gitconfig aliases for Pull Request Managment

Install

Either copy the aliases from the .gitconfig or run the commands in add-pr-alias.sh

Usage

Easily checkout local copies of pull requests from remotes:

  • git pr 4 - creates local branch pr/4 from the github upstream(if it exists) or origin remote and checks it out
  • git pr 4 someremote - creates local branch pr/4 from someremote remote and checks it out
@isaacsanders
isaacsanders / agenda.md
Created January 2, 2013 02:43
An agenda for a recent meeting.

#Council of Chiefs

9:00PM November 3, 2012

Camp Lazarus
4422 Columbus Pike
Delaware, OH 43015

Goal of the Meeting: To make decisions regarding the specifics of Conclave 2013

@piscisaureus
piscisaureus / pr.md
Created August 13, 2012 16:12
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this: