Skip to content

Instantly share code, notes, and snippets.

@chuckg
chuckg / albums
Last active September 24, 2015 02:58
Foobar2k File Move/Copy Syntax
%album artist%/%date% - %album%$if(%discnumber%,[' - (disc '%discnumber%')'])$if(%codec%='FLAC',' [FLAC]')/%tracknumber% -$ifequal(%artist%, %track artist%,[' ('%track artist%') -'],'') %title%
#!/bin/bash
# Copyright 2004 Ben Reser <ben@reser.org>
# Licensed under the terms subversion ships under or GPLv2.
# Useful for greping in a subversion working copy.
# Essentially it behaves the same way your grep command does (in fact it
# ultimately calls the grep command on your path) with a few exceptions.
# Ignores the subversion admin directories (.svn) and vi(m) backup files.
# Recursive is always on with or without -r.
@chuckg
chuckg / gist:1251851
Created September 29, 2011 20:34
Find the largest palindrome in a string the slow way (not O(n)).
sentence = "i am a big radar and i like to play aaa with toys".split.join
largest = ''
for i in 0..(sentence.size - 1)
first_char = sentence[0..0]
sentence = sentence[1..sentence.size]
segment = ''
@chuckg
chuckg / factories.rb
Created January 4, 2012 00:19
Rails 3: Add #attachment FactoryGirl method for defining paperclip attachments on models.
require 'action_dispatch/testing/test_process'
Factory::DefinitionProxy.class_eval do
def attachment(name, path, content_type = nil)
path_with_rails_root = "#{Rails.root}/#{path}"
uploaded_file = if content_type
Rack::Test::UploadedFile.new(path_with_rails_root, content_type)
else
Rack::Test::UploadedFile.new(path_with_rails_root)
end
@chuckg
chuckg / gist:2031981
Created March 13, 2012 21:51
ASUS RT-N66U: Port forwarding issues

Firmware: 3.0.0.3.108

Port forwarding is completely broken. Adding a new port to forward to does not result in the internal device receiving the data and after checking the "System Log" under the "Port Forwarding" tab I'm presented with an empty forwarding table:

Destination     Proto. Port range  Redirect to     Local port

--

class TreeNode
attr_accessor :nodeType, :value, :children
def initialize(nodeType, value, children=[])
raise Exception unless children.is_a?(Array)
@nodeType = nodeType
@value = value
@children = children
end
end
@chuckg
chuckg / spec_helper.rb
Created December 3, 2012 22:29 — forked from pauljamesrussell/spec_helper.rb
RSpec matcher for ensuring a model is accepting nested attributes for an association, and accepting/rejecting the right values.
# Use: it { should accept_nested_attributes_for(:association_name).and_accept({valid_values => true}).but_reject({ :reject_if_nil => nil })}
RSpec::Matchers.define :accept_nested_attributes_for do |association|
match do |model|
@model = model
@nested_att_present = model.respond_to?("#{association}_attributes=".to_sym)
if @nested_att_present && @reject
model.send("#{association}_attributes=".to_sym,[@reject])
@reject_success = model.send("#{association}").empty?
end
model.send("#{association}").clear
@chuckg
chuckg / binary_tree_equality.rb
Last active December 14, 2015 13:19
Binary tree value equality.
require 'tree'
require 'rspec'
# 2
# / \
# 1 4
# /
# 3
#
#
@chuckg
chuckg / capypbara_utilities.rb
Created March 21, 2013 21:21
Capybara `sleep()` is sinful. Instead, block until your element appears!!
def wait_until_element_exists(element)
wait_until { page.find(element).visible? }
return unless block_given?
within element do
yield
end
rescue Capybara::TimeoutError
flunk "Expected '#{element}' to be present."
end
require 'rack/test'
describe SampleMiddleware do
include Rack::Test::Methods
let(:inner_app) do
lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['All good!'] }
end
let(:app) { SampleMiddleware.new(inner_app) }