Skip to content

Instantly share code, notes, and snippets.

View JonathonMA's full-sized avatar

Jonathon M. Abbott JonathonMA

  • Brisbane, Australia
View GitHub Profile
@JonathonMA
JonathonMA / latte_script.rb
Created June 24, 2013 07:00
Wrap coffeescript in a closure
require 'coffee-script'
code = DATA.read
magic_code = <<_EOJS_
_magic_code = ($) ->
#{code}
_magic_code(jQuery);
_EOJS_
puts CoffeeScript.compile magic_code
__END__
def hash_smash bench_size, bm
hash_1 = {}
hash_2 = {}
1.upto(bench_size) do |n|
k = "val#{n}"
hash_1[k] = {
name: k,
total: n * 2,
}
@JonathonMA
JonathonMA / nih_money.rb
Created July 30, 2013 06:00
Money#format is the most complicated thing ever.
module NihMoney
def nih_money_units money
if money.cents % money.currency.subunit_to_unit == 0
money.cents / money.currency.subunit_to_unit
else
money.cents.to_f / money.currency.subunit_to_unit
end
end
def nih_humanized_money_with_symbol money
class Challenge
def user_with_highest stat = :distance
User.with_highest stat, period, users
end
def period
start_date..end_date
end
end
class Symbol
def with(arg)
lambda { |obj| obj.send(self, arg) }
end
end
tweedledee = Class.new.new
tweedledum = Object.new
heroes = [tweedledum, tweedledee]
@JonathonMA
JonathonMA / cross.rb
Last active December 22, 2015 01:39
Cross product of an array against itself n times
def cross values, n = 3
Array.new(n, values).inject do |acc, i|
acc.product(i).map(&:flatten)
end
end
require 'set'
describe do
it "should cross itself" do
@JonathonMA
JonathonMA / post-checkout
Created September 11, 2013 04:20
Git post-checkout hook to enable sensible foreman restart upon branch change with bonus bundle check
#!/usr/bin/env ruby
# post-checkout - git post checkout hook script
#
# This script will update the contents of .git/CURRENT_BRANCH with the name of
# the current branch after a checkout.
#
# Use it with rerun to restart foreman on branch changes.
# Bonus feature: sanity checks your Gemfile using $ bundle check
#
# Start foreman like this to watch CURRENT_BRANCH:
@JonathonMA
JonathonMA / testfor.rb
Created September 13, 2013 06:00
Sick of boilerplate.
#!/usr/bin/env ruby
# testfor - gen test for class
#
# USAGE
#
# Generate a test skeleton for the Person class
# $ testfor Person
require 'active_support'
require 'active_support/core_ext/string/inflections'
@JonathonMA
JonathonMA / mocks.rb
Created September 17, 2013 04:30
F@cking Mocks? How do they work?
# Note minitest 2 doesn't run out of assertions so that test case would fail
gem 'minitest', '~> 4'
require 'minitest/unit'
require 'minitest/mock'
require 'minitest/autorun'
def do_nothing_with object
end
@JonathonMA
JonathonMA / wat_errors.rb
Created September 18, 2013 06:41
Errors, how do they work?
begin
fail NoMethodError, "foo"
rescue StandardError => e
puts "NoMethodError is rescued by StandardError: #{e.inspect}"
rescue NoMethodError => e
puts "NoMethodError is not rescued by StandardError: #{e.inspect}"
p e
end
puts "NoMethodError is not a StandardError" unless NoMethodError.is_a? StandardError