Skip to content

Instantly share code, notes, and snippets.

Spec::Matchers.define :be_invalid_on do |field|
match do |model_instance|
!model_instance.valid? && model_instance.errors.on(field) != nil
end
end
require 'benchmark'
d1 = [1,1,1,1,0,0,0,0,0]
d2 = [1,0,1,0,1,1,0,0,0]
d3 = [0,0,0,0,1,0,1,1,1]
def calc_sim vector1, vector2
use Rack::Logger
app = proc do |env|
[ 200, {'Content-Type' => 'text/plain'}, "a" ]
end
run app
#This will produce:
# Rack::Lint::LintError: rack.errors#close must not be called
#Output
#Jerome-Gagners-MacBook-Pro:amazon jgagner$ ruby bids.rb
#Current bid: 6, bid total 6, current max 0
#Current bid: 6, bid total 12, current max 6
#Current bid: 6, bid total 18, current max 12
#Current bid: 3, bid total 12, current max 18
#Accepted bids = 666 at 18
#Current bid: 6, bid total 6, current max 0
#Current bid: 5, bid total 10, current max 6
#Current bid: 4, bid total 12, current max 10
//Squished a couple files together
package com.jeromegagner.example.trees.binary;
/**
* Created by IntelliJ IDEA.
* User: jgagner
* Date: Apr 18, 2010
* Time: 1:30:24 PM
* To change this template use File | Settings | File Templates.
There are four ways to do something
<ul>
<% (1..4).zip([:a,:b,:c,:d]).zip(["face1","face2","face3","face4"]).transpose[0].transpose[0].each do |x| %>
<li><%= x %> </li>
<% end %>
</ul>
def do_something &block
(1..4).zip([:a,:b,:c,:d]).zip((["face"] * 4).map {|x| x.to_sym}).transpose[0].transpose[0].each(&block)
end
do_something { puts "face!"}
#doesn't accept args for the methods... probably next on that
class Object
def try_chain(*args)
method = args.shift
if respond_to?(method)
args.empty? ? self.send(method) : self.send(method).try_chain(*args)
else
nil
end
end
#mostly pseudo-code
#LOLOLOL
class Module
alias_method :old_method_missing, :method_missing
def method_missing(methId)
methods = methId.split("_")
tmp = self
while methods.size > 0
my_meth = methods.pop
@jgagner
jgagner / gist:5809237
Created June 18, 2013 20:50
Ordering du by size. Found on the internet here http://ubuntuforums.org/showthread.php?t=885344
du -k * | sort -nr | cut -f2 | xargs -d '\n' du -sh | less