Skip to content

Instantly share code, notes, and snippets.

@puyo
puyo / capybara_find_with_info.rb
Created January 31, 2014 04:45
More useful output from Capybara
# More useful output from Capybara.
#
# Before:
#
# Unable to find link or button "Year 5"
#
# After:
#
# Unable to find link or button "Year 5" in "Sorry, something went wrong We've
# been notified of the error and are looking into it."
@puyo
puyo / encamel.rb
Last active August 29, 2015 13:57
#!/usr/bin/env ruby
gem 'activesupport'
require 'active_support/core_ext/string/inflections'
snake = ARGV.first
camel = snake.camelize(:lower)
system %{git grep -l #{snake} | xargs ruby -p -i -e '$_.gsub! /#{snake}/, "#{camel}"'}
@puyo
puyo / alp_facebook_popularity.rb
Last active August 29, 2015 13:58
Run a Facebook based popularity competition for ALP MPs
gem 'activesupport' # {}.to_query
gem 'nokogiri'
gem 'typhoeus'
require 'active_support/core_ext/object'
require 'json'
require 'nokogiri'
require 'pp'
require 'typhoeus'
require 'rubygems' rescue nil
require 'gosu'
require 'chipmunk'
class Array
# e.g. [1,2,3].each_link yields [1,2], [2,3]
def each_link
prev = first
self[1, size].each do |item|
yield prev, item
# What is it with you people and your singletons?
class Hash
def self.from_block(&block)
Hash[*BlockBuilder.new(&block).collected]
end
class BlockBuilder
attr_reader :collected
# Is there a preferred way to do it? A lot of people I see use inject, but it makes my eyes bleed.
x = [[1, 2], [3, 4]]
p x.inject({}) { |h, r| h[r[0]] = r[1]; h }
# {1=>2, 3=>4}
p Hash[*x.flatten]
# {1=>2, 3=>4}
describe 'rsync' do
it 'should keep the latest version of a file' do
system("mkdir -p d1 d2")
begin
system("echo 'This is the first version' > d1/file.txt")
sleep 1 # ensure timestamps are different
system("echo 'This is the second version' > d2/file.txt")
system("rsync -ru d2/ dest/")
system("rsync -ru d1/ dest/")
File.read('dest/file.txt').should include('second version')
@puyo
puyo / mypresentation.haml
Created November 19, 2010 22:18
Putting all your slick in one place...?
:javascript
Shining.config({...})
#first
%h1 Hi there world
%p Yes this is a slide
%p.step with a step
:javascript
// stuff for the first slide
:css
@puyo
puyo / order_spec.rb
Created August 26, 2011 08:08
Is it possible to rspec this properly without hitting the database?
create_table "line_items", :force => true do |t|
t.integer "cost"
t.boolean "paid"
t.integer "order_id"
end
create_table "orders", :force => true do |t|
end
# -----
@puyo
puyo / Gemfile
Created September 6, 2011 03:07
Rails 3.1 + Sprockets + Compass
gem 'haml-rails', '~> 0.3'
gem 'rails', '~> 3.1'
group :assets do
gem 'compass', '~> 0.11'
gem 'sass-rails', '~> 3.1'
gem 'coffee-rails', '~> 3.1'
gem 'uglifier', '~> 1.0'
end