Skip to content

Instantly share code, notes, and snippets.

@puyo
puyo / tkinter_raise.py
Created November 29, 2013 04:22
A little bit of code to focus the window when running Python Tkinter programs from the command line on OSX.
import sys, os, threading
if sys.platform == 'darwin':
def raise_window():
os.system("""osascript -e 'tell app "Finder" to set frontmost of process "Python" to true'""")
threading.Timer(0.1, raise_window).start()

RSpec 3 syntax is more verbose.

About twice as many characters to type to stub something:

obj.stub(client: client)                          # old
allow(obj).to receive(:client).and_return(client) # new
allow(obj).to receive(client: client)             # possible? still much longer
allow(obj, client: client)                        # I might wrap it in this
@puyo
puyo / rspec-update.rb
Created November 14, 2013 23:22
Madness. Complete madness. But it mostly worked. A ghetto version of https://github.com/yujinakayama/transpec ;-)
require 'pathname'
system('git checkout .')
Pathname.glob('spec/**/*_spec.rb') do |path|
#next if path.to_s.match(/tsa_open_period_spec|tutor_earning_spec|tutoring_service_agreement_spec|presence_spec/)
content = path.read
content.gsub!(/\.should ==\s*(.+?)(\s#\s|$)/, %q{.should eq(\1)\2})
content.gsub!(/\.should_not ==\s*(.+?)(\s#\s|$)/, %q{.should_not eq(\1)\2})
def f(x = []):
x.append(1)
return x
print f() # [1]
print f() # [1, 1]
print f() # [1, 1, 1]
print f() # [1, 1, 1, 1]
@puyo
puyo / calling_it.rb
Last active December 20, 2015 12:19
One liner expect..to..change One liner should_receives One liner to rule them all and in the darkness bind them
module CallingIt
module ExampleGroupClassMethods
def subject(name=nil, &block)
# Add extra methods that allow calling the block that was passed, in the
# example's lexical scope.
if block
define_method(:calling_it) do
lambda { instance_eval(&block) }
end
alias_method(:subject_call, :calling_it)
@puyo
puyo / gist:6027330
Last active December 19, 2015 22:28
Make PDFKit middleware set content disposition so that .pdf links are downloaded.
require 'pdfkit'
class PDFKitMiddlewareDownload < PDFKit::Middleware
def call(env)
status, headers, response = super(env)
if headers['Content-Type'] == 'application/pdf'
headers['Content-Disposition'] = 'attachment'
end
@puyo
puyo / subwords.rb
Created March 3, 2013 22:29
Gabe: Find the word that has the most sub-words. Words that can be made out of the same letters.
# Gabe: Find the word that has the most sub-words. Words that can
# be made out of the same letters.
def chars(str)
str.split('')
end
class Node
attr_reader :letter, :leaf, :yes, :no
@puyo
puyo / bigwords.rb
Last active September 20, 2016 07:09
"Find the biggest words you can make out of a random set of letters."
# Elle: just thinking: what about if you ignore all the words
# that have letters that are not part of your letters group
# so it is less words to go and process.
#
# So we want an index that helps us ignore words that are not going
# to match, then do our expensive match logic on what's left.
#
# e.g.
#
# words = bat bath tart
@puyo
puyo / coins.rb
Last active December 13, 2015 18:39
Incrementally improving my recursive solution to the problem: "How many combinations of 25c, 10c and 5c coins are there that add up to 75c?"
#!/usr/bin/env ruby
def coins1(left, collection = [])
if left == 0
[collection.sort]
elsif left >= 25
coins1(left - 25, collection + [25]) +
coins1(left - 10, collection + [10]) +
coins1(left - 5, collection + [5])
elsif left >= 10
@puyo
puyo / _responsive.sass
Created November 3, 2012 06:00
A responsive design SASS mixin that reduces repetition in the CSS output
$responsive-small-width: 480px !default
$responsive-medium-width: 768px !default
$responsive-slow-device-width1: 480px !default
$responsive-slow-device-width2: 1024px !default
$responsive-fast-device-width: 1024px !default
// This mixin is to be used a bit like at-breakpoint from Susy [1] or the
// breakpoint mixin from CSS Tricks [2], however it reduces repetition in the
// outputted css by allowing you to pass several breakpoint names at once and
// compiling a media query that matches any of them, for your content.