Skip to content

Instantly share code, notes, and snippets.

Avatar
🏗️
🗳️🌱🌷🌐🏗️🏓☢️

Clay Shentrup ClayShentrup

🏗️
🗳️🌱🌷🌐🏗️🏓☢️
View GitHub Profile
@ClayShentrup
ClayShentrup / gist:2528600
Created Apr 29, 2012
Devil's advocate argument for having the case statement in Ruby
View gist:2528600
case some_value
when 1..10
"do some stuff here"
when 11..20
"do something else here"
else
"out of range"
end
# compared to
@ClayShentrup
ClayShentrup / wrap.rb
Created Jun 26, 2012 — forked from garybernhardt/wrap.rb
Probably a really bad implementation of word wrapping
View wrap.rb
class Wrap < Struct.new(:string, :max_length)
def self.wrap(s, max_length)
raise ArgumentError.new("Maximum wrap length can't be 0") if max_length == 0
new(s, max_length).wrap
end
def wrap
return [""] if blank?
string.scan(regexp)
end
View whale_test.rb
require 'test/unit'
class Whale
def self.attr_validated(method_name, &validation)
# Enter code here to make all tests pass.
end
attr_validated :num_teeth do |v|
v <= 4
end
View beget.js
function beget(parent, child){
// put code here to make test() return true
};
function test(){
function Parent(){
throw 'exception';
};
Parent.prototype = {code: Math.random()};
@ClayShentrup
ClayShentrup / buckets_controller_spec.rb
Created Dec 16, 2012
Pristine RSpec controller spec using latest syntax and general minimalism
View buckets_controller_spec.rb
require 'spec_helper'
describe BucketsController do
render_views
login_admin
def valid_attributes
FactoryGirl.attributes_for(:bucket)
end
@ClayShentrup
ClayShentrup / stub.go
Created Nov 3, 2013
Stubbing in Go (Golang)
View stub.go
package main
import (
"fmt"
"reflect"
"time"
)
func main() {
stubPrototype := func(in []reflect.Value) []reflect.Value {
@ClayShentrup
ClayShentrup / original.rb
Created Nov 16, 2013
Trying to understand the point of Gary Bernhardt's "Boundaries" talk
View original.rb
describe Sweeper do
context 'a subscription is expired' do
let(:bob) { double(active?: true, paid_at: 2.months.ago) }
let(:users) { [bob] }
before { allow(User).to receive(:all).and_return(users) }
it 'emails the user' do
expect(UserMailer).to receive(:billing_problem).with(bob)
described_class.sweep
end
@ClayShentrup
ClayShentrup / spec_example.rb
Created Dec 2, 2015
Testing that code executes within a block, e.g. transaction or time zone
View spec_example.rb
it('uses the client time zone') do
allow(described_class).to receive(:delete_cache) do
fail unless Time.zone == client_time_zone
end
allow(described_class).to receive(:create_cache) do
fail unless Time.zone == client_time_zone
end
update_for_year
end
@ClayShentrup
ClayShentrup / car.rb
Last active Apr 22, 2017
Skipping ActiveRecord association validations in tests
View car.rb
class Car < ActiveRecord::Base
attr_accessor(:skip_association_presence_validations)
validates(:driver, presence: true, unless: :skip_association_presence_validations)
# creates driver_license_number method
delegate(:license_number, to: :driver, prefix: true)
end
View results.txt
expected collection contained: [#<User id: 1, email: "user1@example.com", ...a bunch of attributes...>]
actual collection contained: [#<User id: 1, email: "user1@example.com", ...a bunch of attributes...>,
#<User id: 2, email: "user2@example.com", ...a bunch of attributes...>]