Skip to content

Instantly share code, notes, and snippets.

View timuruski's full-sized avatar
🏳️‍⚧️
Protect Trans Rights

Tim Uruski timuruski

🏳️‍⚧️
Protect Trans Rights
View GitHub Profile
@timuruski
timuruski / mutable_default.py
Created September 2, 2011 17:46
Ruby and Python handle mutable arguments differently, inspired by http://blog.extracheese.org/2006/12/pythons-default-arguments-are-tricky.html
def foo(d=[]):
d.append('a')
return d
print foo() # => ['a']
print foo() # => ['a', 'a']
print foo() # => ['a', 'a', 'a']
I'm putting this up here so that I can see that the GitHub Gist CLI tool works properly.
@timuruski
timuruski / add-submodules.sh
Created September 14, 2011 15:15
A command line that walks vim/bundle directory and adds each git-submodule.
#! /bin/bash
find vim/bundle -name "config" | grep .git | while read d; do echo "submodule add " $(grep "url = " $d | awk '{print $3}') $(echo $d | sed -e "s/\.git\/config//") | xargs git; done
@timuruski
timuruski / rr-blocks.rb
Created October 13, 2011 23:05
RR has block mode.
image_a = Image.make
stub(image_a).migrated? { false }
stub(image_a).local_filename { 'spec/fixtures/backdrop.jpg' }
image_b = Image.make
stub(image_b).migrated? { false }
stub(image_b).local_filename { 'spec/fixtures/backdrop.jpg' }
document = Movie.new(:title => 'Test Movie', :images => [image_a, image_b])
// Operational
{
"operational": true,
"message": null
}
// Failing
{
"operational": false,
"message": "We'll be right back."
@timuruski
timuruski / gist:1603465
Created January 12, 2012 22:14
Not surprising, but slightly fun math thing
x = 129
a = (0...x).map { |n| (n % x) }.uniq
puts a.size == x # true
x = 129
a = (0...(x-1)).map { |n| (n % x) }.uniq
puts a.size == x # false
class Post
def self.find_by_id(id)
self.new
end
def comments
CommentCollection.new
end
end
# As illustrated in http://confreaks.net/videos/427-rubyconf2010-zomg-why-is-this-code-so-slow
Person = Struct.new(:name)
people = [Person.new('steve'), Person.new('bill'), Person.new('larry')]
puts people.inject({}) { |table,person| table[person.name] = person; table }
puts Hash[people.map { |person| [person.name, person] }]
# {"steve"=>#<struct Person name="steve">, "bill"=>#<struct Person name="bill">, "larry"=>#<struct Person name="larry">}
describe "validations" do
context "birthday" do
["bad format","2100-01-01","1800-01-01","2000-13-01","2000-00-01","2000-01-32","2000-01-00"].each do |bday|
it "errors with invalid date - #{bday}" do
person = Person.create(:name => "not blank", :birthday => bday)
person.errors.messages[:birthday].first.should == "is invalid"
end
end
["2099-12-31", nil].each do |bday|
class Order < ActiveModel::Base
module States
UNPAID = 'unpaid'.freeze
PAID = 'paid'.freeze
end
attr_accessor :state
validates :state, :inclusion => { :in => [States::UNPAID, States::PAID] }
end