Skip to content

Instantly share code, notes, and snippets.

robmiller / sample-n.rb
Last active Aug 29, 2015
Select a random n values from an array that might have < n values
View sample-n.rb
words = %w{foo bar baz}
n = 10
# If we use sample, we get a random order but never
# more than 3 entries:
# => ["bar", "foo", "baz"]
# A solution: { words.sample }
View gist:9451784
def method_a
method_b do |thing|
thing = 'hey'
def method_b
thing = nil
thing = yield thing
robmiller / av
Last active Aug 29, 2015
Take a webcam shot and save it in the pictures directory. Depends on tenderlove's av_capture: `gem install av_capture`
View av
#!/usr/bin/env ruby
require 'av_capture'
require 'pathname'
session =
dev = AVCapture.devices.find(&:video?)
dir = Pathname(File.expand_path("~/Pictures/av"))
Dir.mkdir(dir) unless dir.exist?

What is AOP?

Aspect Oriented Programming is a means to change the behaviour of – or add behaviour to – methods and functions (including constructors) non-invasively. The added behaviour is called “advice” and can be added before, after, or around the function it advises.

This description is similar to the Extract Surrounding refactoring method. The difference is in the direction of the change. It seems AOP is more focused at modifying existing behaviour non-invasively; where as the Extract Surrounding Method actually changes the source code to allow this type of behavioural modification.

robmiller / mutant.diff
Last active Aug 29, 2015
mutant exception example
View mutant.diff
# foo.rb
def foo
@db =
rescue PStore::Error
raise"Database directory #{data_file} does not exist")
# foo_spec.rb
describe "#foo" do
it "should raise an error when database directory is inaccessible" do
robmiller / golf.rb
Last active Aug 29, 2015
Yammer Ruby Hacknight Golf
View golf.rb
n=0;'a.reduce :*
(1..a).reduce :*{|s|z=s[0];z==?m?"hat(#{s})":z==?d?s[0..-2]+"(bone))":"dead"+s[3..-1]}
i,j=0,1;(1..a).map{i=j+j=i}{|s|s.size>10?s[0,4]+?.*3+s[-3,3]:s}.join" "'
robmiller / gist:d32dc4826639b69c92e7
Created May 23, 2014
Script, designed to be run on a cron job, to scan for files (in this case PHP files) that have been modified and notify a Slack channel with the changes. Just for information, not for proper intrusion detection or anything
View gist:d32dc4826639b69c92e7
#!/usr/bin/env ruby
require 'net/http'
require "json"
require "pathname"
require "logger"
class Slack
attr_reader :account, :token, :channel
robmiller / metakoans.rb
Created Jun 19, 2014
Solution to Ara T. Howard's metakoans.rb Ruby Quiz:
View metakoans.rb
class Module
def attribute(attribute, &block)
case attribute
when Hash
name = attribute.keys.first
default = attribute[name]
when String, Symbol
name = attribute
default = nil
robmiller /
Last active Aug 29, 2015
Project Euler #3 in Rust
struct PrimeStream {
current: int
impl PrimeStream {
fn new() -> PrimeStream {
PrimeStream { current: 1 }
robmiller /
Created Jul 30, 2014
Random code generation in Rust
use std::rand::{task_rng, Rng};
use std::os;
use std::collections::hashmap::HashSet;
fn main() {
let num_threads = 8u;
let args = os::args();
let usage = "Usage: code-generator NUMCODES CODEFORMAT";