Skip to content

Instantly share code, notes, and snippets.

@robmiller
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:
words.sample(n)
# => ["bar", "foo", "baz"]
# A solution:
n.times.map { words.sample }
View gist:9451784
def method_a
method_b do |thing|
thing = 'hey'
end
end
def method_b
thing = nil
thing = yield thing
@robmiller
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 = AVCapture::Session.new
dev = AVCapture.devices.find(&:video?)
dir = Pathname(File.expand_path("~/Pictures/av"))
Dir.mkdir(dir) unless dir.exist?
View AOP.md

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 Aug 29, 2015
mutant exception example
View mutant.diff
# 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 Aug 29, 2015
Yammer Ruby Hacknight Golf
View golf.rb
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
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
robmiller / metakoans.rb
Created Jun 19, 2014
Solution to Ara T. Howard's metakoans.rb Ruby Quiz: http://rubyquiz.com/quiz67.html
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
end
@robmiller
robmiller / 003.rs
Last active Aug 29, 2015
Project Euler #3 in Rust
View 003.rs
struct PrimeStream {
current: int
}
impl PrimeStream {
fn new() -> PrimeStream {
PrimeStream { current: 1 }
}
}
@robmiller
robmiller / code-generator.rs
Created Jul 30, 2014
Random code generation in Rust
View code-generator.rs
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";