Skip to content

Instantly share code, notes, and snippets.

@whylom
whylom / spider.js
Created December 24, 2014 16:15
dot.js spider checking a site for broken images
function advance() {
var url = document.location.href;
var parts = url.split('/');
var id = parts[parts.length-1];
var nextID = parseInt(id) + 1;
document.location = "https://site.com/resources/" + nextID;
}
var image = $('img.should-be-here');
@whylom
whylom / README.md
Created December 4, 2014 23:43
A parser for Heroku log messages

A parser for Heroku log messages

This parser uses the Parslet to define rules for parsing a log message in the simple key=value format used by Heroku, eg:

at=error code=H12 desc="Request timeout" method=GET path="/foo" dyno=web.3 connect=1ms service=30000ms status=503 bytes=0

Given a message in the above format, the parser returns a hash of the key/value pairs:

@whylom
whylom / count.rb
Created October 24, 2014 22:18
Count instances of each member of an array
# given an array
words = %w(duck duck goose cry baby cry)
# generate a hash that counts occurrences of each word, eg:
# {"duck"=>2, "goose"=>1, "cry"=>2, "baby"=>1}
# Method #1
counts = Hash.new(0)
words.each { |word| counts[word] += 1 }
### Keybase proof
I hereby claim:
* I am whylom on github.
* I am whylom (https://keybase.io/whylom) on keybase.
* I have a public key whose fingerprint is B2D6 34F6 025A 0830 0169 3C32 80F6 B36C A0C7 7A66
To claim this, I am signing this object:
@whylom
whylom / README.md
Last active August 29, 2015 13:57
Superhash!

Superhash!

A wrapper for Hash that lets you access keys via dynamic methods (like Ruby's OpenStruct) but also:

  • works recursively
  • also lets you use Hash methods like [] and keys

Example

@whylom
whylom / to_hash.rb
Last active December 25, 2015 20:19
Array#to_hash converts an array of instances into a hash using the provided pair of methods to determine the key & value of each row
# example class
class State < Struct.new(:name, :code)
def self.all
[
State.new('Alabama', 'AL'),
State.new('Alaska', 'AK'),
State.new('Arizona', 'AZ')
]
end
end
@whylom
whylom / sync.rake
Last active March 22, 2016 19:44
Rake task to copy assets between S3 buckets. Requires the aws-s3 gem (http://amazon.rubyforge.org/)
# add a new "put_copy" method to the Amazon client's S3Object class
# to enable copying an object from 1 bucket to another
# http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html
class AWS::S3::S3Object
def self.put_copy(source_key, target_key, source_bucket, target_bucket, options = {})
original = open(url_for(source_key, source_bucket))
default_options = { :content_type => original.content_type }
store(target_key, original, target_bucket, default_options.merge(options))
acl(target_key, target_bucket, acl(source_key, source_bucket))
end
@whylom
whylom / .powrc
Created September 26, 2013 15:47
.powrc for RVM gemset
if [ -f "$rvm_path/scripts/rvm" ]; then
source "$rvm_path/scripts/rvm"
rvm use $(cat .ruby-version)@$(cat .ruby-gemset)
fi
@whylom
whylom / README.md
Last active December 17, 2015 19:49
Ruby script that enhances the standard `git branch` listing by organizing and color coding remotes (origin, root, etc).

This is a Ruby script that enhances the standard git branch listing by organizing and color coding remotes (origin, root, etc).

screenshot

  • Branches whose remote is origin (the default) are listed at the top.
  • Branches whose remote is not origin are listed next, with the remote displayed in magenta. This makes it easier to see branches that are shared with other developers.
  • A hardcoded list of staging branches are displayed at the bottom in gold.
@whylom
whylom / trick.rb
Created January 17, 2013 19:28
Neat Sinatra trick
set(:probability) { |value| condition { rand <= value } }
get '/win_a_car', :probability => 0.1 do
"You won!"
end
get '/win_a_car' do
"Sorry, you lost."
end