Skip to content

Instantly share code, notes, and snippets.

@robmiller
robmiller / sample-n.rb
Last active August 29, 2015 13:55
Select a random n values from an array that might have < n values
words = %w{foo bar baz}
n = 10
# If we use sample, we get a random order but never
# more than 3 entries:
words.sample(n)
# => ["bar", "foo", "baz"]
# A solution:
n.times.map { words.sample }
def method_a
method_b do |thing|
thing = 'hey'
end
end
def method_b
thing = nil
thing = yield thing
@robmiller
robmiller / av
Last active August 29, 2015 13:57
Take a webcam shot and save it in the pictures directory. Depends on tenderlove's av_capture: `gem install av_capture`
#!/usr/bin/env ruby
require 'av_capture'
require 'pathname'
session = AVCapture::Session.new
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
robmiller / mutant.diff
Last active August 29, 2015 14:00
mutant exception example
# foo.rb
def foo
@db = PStore.new(data_file)
rescue PStore::Error
raise DatabaseError.new("Database directory #{data_file} does not exist")
end
# foo_spec.rb
describe "#foo" do
it "should raise an error when database directory is inaccessible" do
@robmiller
robmiller / golf.rb
Last active August 29, 2015 14:00
Yammer Ruby Hacknight Golf
class<<Golf=Class.new
n=0;'a.reduce :*
(?a..?z).to_a
(1..a).reduce :*
a.map{|s|z=s[0];z==?m?"hat(#{s})":z==?d?s[0..-2]+"(bone))":"dead"+s[3..-1]}
(1..4).flat_map{|n|a.each_cons(n).to_a}
(1..a).map{|a|a%15<1?"fizzbuzz":a%3<1?"fizz":a%5<1?"buzz":a}
p=0;a.slice_before{|n|t=p+1<n;p=n;t}.map{|n|z="#{n[0]}";n.size>1?z+?-+"#{n[-1]}":z}
i,j=0,1;(1..a).map{i=j+j=i}
a.split.map{|s|s.size>10?s[0,4]+?.*3+s[-3,3]:s}.join" "'
@robmiller
robmiller / gist:d32dc4826639b69c92e7
Created May 23, 2014 15:08
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
#!/usr/bin/env ruby
require 'net/http'
require "json"
require "pathname"
require "logger"
class Slack
attr_reader :account, :token, :channel
@robmiller
robmiller / metakoans.rb
Created June 19, 2014 18:27
Solution to Ara T. Howard's metakoans.rb Ruby Quiz: http://rubyquiz.com/quiz67.html
class Module
def attribute(attribute, &block)
case attribute
when Hash
name = attribute.keys.first
default = attribute[name]
when String, Symbol
name = attribute
default = nil
end
@robmiller
robmiller / 003.rs
Last active August 29, 2015 14:03
Project Euler #3 in Rust
struct PrimeStream {
current: int
}
impl PrimeStream {
fn new() -> PrimeStream {
PrimeStream { current: 1 }
}
}
@robmiller
robmiller / code-generator.rs
Created July 30, 2014 09:45
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";