Skip to content

Instantly share code, notes, and snippets.

@cupakromer
cupakromer / gist:3879718
Created October 12, 2012 15:20
WTF rails?
class MyModel < ActiveRecord::Base
  has_many :things
end

model = MyModel.new
model.things.build do
  #stuff...
end
@cupakromer
cupakromer / gist:3881956
Created October 12, 2012 22:24
Rails Bug Issue Draft
class Cart < ActiveRecord::Base
  has_many :line_items

  def total
    line_items.sum(&:price)
  end
end

class LineItem &lt; ActiveRecord::Base
@cupakromer
cupakromer / gist:3900265
Created October 16, 2012 16:08
RSpec good idea?
describe CommentsController do
describe '#create' do
context 'with valid form' do
let(:expect_commenting) { expect{ post :create, comment: { user_id: 1, body: "This is a comment" } } }
subject {
post :create, comment: { user_id: 1, body: "This is a comment" }
@controller
}
@cupakromer
cupakromer / .rspec
Created November 1, 2012 21:07
Global RSpec (in ~/)
--format nested
--order rand
--colour
# If using spork: --drb
@cupakromer
cupakromer / gist:4069649
Created November 14, 2012 01:28
Pry Session Dump
class Test < SimpleDelegator
def initialize
end
end
#=> nil
t = Test.new
#=> nil
t[:thing] = 'help'
@cupakromer
cupakromer / _simple_rspec.rb
Created December 10, 2012 00:16
Simple RSpec
#!/usr/bin/env ruby
#encoding: UTF-8
################################################################################
################################################################################
################################################################################
# Overly simplistic implementation of how a RSpec test works #
################################################################################
@cupakromer
cupakromer / gist:4253406
Created December 10, 2012 21:03
Hash Benchmarks
#!/usr/bin/env ruby
require 'benchmark'
require 'digest'
n = 5_000_000
xruns = 10
puts <<-EOS
Hash Benchmarks
===============
@cupakromer
cupakromer / solution1.c
Last active October 13, 2015 22:18
Array Jump solution
#define MAX_STEPS 100000
#define NO_SOLUTION -1
int arrayJump(int *A, int size) {
int *p = A, steps = 0;
while (++steps < MAX_STEPS && A <= (p+=*p) && p < A+size);
return steps >= MAX_STEPS ? NO_SOLUTION : steps;
}
@cupakromer
cupakromer / Compiling
Created December 19, 2012 17:19
Attempt at SHA256 in C
Compile with `gcc -Wall -lssl -lcrypto shait.c`
@cupakromer
cupakromer / gist:4342420
Created December 20, 2012 02:07
A little SHA in the C
char *sha256_hexdigest(const void *buf_to_hash, size_t buf_len, char *hexdigest)
{
uint8_t binary_hash[SHA256_DIGEST_LENGTH];
uint8_t *digest;
SHA256_CTX context;
int i;
SHA256_Init(&context);
SHA256_Update(&context, buf_to_hash, buf_len);
SHA256_Final(binary_hash, &context);