Skip to content

Instantly share code, notes, and snippets.

@nahurst
nahurst / file_name_expansion.sh
Created January 17, 2011 19:32
Bash file name expansion
# make a quick backup
cp file{,.bak}
# access files with the same name in different directories
git add app/views/{users,roles}/{index,new}.html
@nahurst
nahurst / bit_hacks.c
Created January 17, 2011 19:34
Bit Hacks
v &= v - 1; // clear the least significant bit set
((x ^ y) < 0); // true iff x and y have opposite signs
//more here http://graphics.stanford.edu/~seander/bithacks.html
@nahurst
nahurst / and_or.rb
Created January 27, 2011 19:26
Ruby and/or vs &&/||
# "and" and && are not synonymous in Ruby
# "and" and "or" have a much lower precedence
# "and" and "or" are more like control flow operators than boolean operators
do_this() or raise "Failure"
assign_me = 10 and assign_me / 2 # (assign_me = 10) and assign_me / 2 ... => 5
#vs
assign_me = 10 && assign_me / 2 # assign_me = (10 && assign_me) / 2 ... => undefined
@nahurst
nahurst / repo_browsers.sh
Created January 27, 2011 19:37
Repository Viewer
# Browse your local git repository
# If you prefer not to use webrick, you could install lighttpd and then just run git instaweb.
git instaweb --httpd webrick # view at http://localhost:1234
# Browse your local Mercurial repository
hg serve # view at http://localhost:8000
@nahurst
nahurst / lcs.js
Created February 21, 2011 21:27
Longest Common Subsequence
/**
* Find a longest common subsenquence.
*
* Note: this is not necessarily the only possible longest common subsequence though!
*/
function lcs(listX, listY) {
return lcsBackTrack(
lcsLengths(listX, listY),
listX, listY,
listX.length, listY.length);
@nahurst
nahurst / rails_time.rb
Created April 24, 2011 17:17
Rails Time
# ActiveSupport::TimeWithZone is a rails construct. Always use it for querying and creating.
# DateTime is an old class and is very Ruby specific (not Rails). Don't use it.
# If you put .utc on the end of something other than a DateTime, it's converted to a Time.
# <class> <to_s>
Time.now # Time Sun Apr 24 13:00:00 -0400 2011
Time.now.utc # Time Sun Apr 24 17:00:00 UTC 2011
Time.now.in_time_zone('UTC') # ActiveSupport::TimeWithZone 2011-04-24 17:00:00 UTC
Time.zone.now # ActiveSupport::TimeWithZone 2011-04-24 17:00:00 UTC
Time.zone.now.utc # Time Sun Apr 24 17:00:00 UTC 2011
@nahurst
nahurst / mirror.sh
Created May 17, 2011 22:18
Mirror a site
wget -w 1 -r -k -K -H -N -l 2 http://example.com # pause 1 second between requests, go 2 levels deep
@nahurst
nahurst / javascript.rb
Created May 27, 2011 00:02
Selenium, rspec, capybara integration with steak to make it like cucumber when cleaning the database javascript tests
# spec/acceptance/support/javascript.rb
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before :each do
Capybara.current_driver = :selenium if example.metadata[:js]
if Capybara.current_driver == :rack_test
DatabaseCleaner.strategy = :transaction
else
DatabaseCleaner.strategy = :truncation
@nahurst
nahurst / show_queries.rb
Created June 1, 2011 20:02
Show queries in rails console
ActiveRecord::Base.logger = Logger.new(STDOUT)
@nahurst
nahurst / inject.rb
Created July 21, 2011 00:25
Simple Ruby Injection
(1..10).inject([]) {|all, i| all << "i#{i}"}
# => ["i1", "i2", "i3", "i4", "i5", "i6", "i7", "i8", "i9", "i10"]