Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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

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 / regex-o.rb
Last active March 3, 2017 03:14
Explaining Ruby regex's /o modifier
require "benchmark"
def letters
puts "letters() called"
sleep 0.5
"A-Za-z"
end
words = %w[the quick brown fox jumped over the lazy dog]
@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?
def method_a
method_b do |thing|
thing = 'hey'
end
end
def method_b
thing = nil
thing = yield thing
@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 }
@robmiller
robmiller / gist:8651287
Created January 27, 2014 16:03
Prepend line to files in shell
# requires sponge, from joey hess's moreutils
# http://joeyh.name/code/moreutils/
for i in *.txt; do; echo "line to prepend\n" "$(cat $i)" | sponge $i; done
@robmiller
robmiller / .vimrc
Last active January 2, 2016 20:29
Match Chris Coyier's "words to avoid in tech writing", adapted from @pengwynn's original
highlight TechWordsToAvoid ctermbg=red ctermfg=white
function MatchTechWordsToAvoid()
match TechWordsToAvoid /\c\<\(obviously\|basically\|simply\|of\scourse\|clearly\|just\|everyone\sknows\|however\|so,\|easy\)\>/
endfunction
autocmd FileType markdown call MatchTechWordsToAvoid()
autocmd BufWinEnter *.md call MatchTechWordsToAvoid()
autocmd InsertEnter *.md call MatchTechWordsToAvoid()
autocmd InsertLeave *.md call MatchTechWordsToAvoid()
autocmd BufWinLeave *.md call clearmatches()