Skip to content

Instantly share code, notes, and snippets.

@nahurst
nahurst / gist:3200127
Created July 29, 2012 16:41
Recovering from corrupted Git
Get the previous commit hash from the last line of .git/logs/HEAD
Replace .git/refs/heads/yourbranch with it
"git status" should now work. Note the files changed (you will copy them later)
Push this repo elsewhere (a different branch on origin)
Remove the .git directory
Pull that repo back out to a different directory
Copy over the noted files to that directory
Commit them
@nahurst
nahurst / rand.rb
Created February 12, 2012 02:23
More convenient random ranges
# Ruby 1.9.2
Random.new.rand(10..15) # 10, 11, 12, 13, 14, 15
Random.new.rand(10...15) # 10, 11, 12, 13, 14
# Ruby >1.9.2
rand(10..15)
@nahurst
nahurst / expand.rb
Created February 12, 2012 02:18
Expand a sequence to an array
[*1..5] # => [1, 2, 3, 4, 5]
@nahurst
nahurst / occurrence_count.rb
Created February 3, 2012 21:52
Order by occurrence count and list count of an array
days = ["Friday", "Saturday", "Sunday", "Monday", "Friday", "Saturday", "Friday"]
days.inject({}) {|h,day| h[day] = h.include?(day) ? h[day]+1 : 1; h }.sort_by{|k,v|v}.reverse.each{|k,v| puts [k,v].join(",")}
#Friday,3
#Saturday,2
#Sunday,1
#Monday,1
@nahurst
nahurst / datestamp_add_token_authenticatable_to_users.rb
Created December 10, 2011 01:30
Add TokenAuthenticatable to an existing user model for devise
# remember to add "devise :token_authenticatable" to user.rb
# and uncomment "config.token_authentication_key = :auth_token" from devise.rb
class AddTokenAuthenticatableToUsers < ActiveRecord::Migration
def self.up
change_table(:users) do |t|
t.token_authenticatable
end
User.reset_column_information
User.all.each do |user|
@nahurst
nahurst / quick_branch.sh
Created October 26, 2011 16:01
Quick temporary feature branch and merge back
git checkout master
git merge --squash private_feature_branch
git commit -v
@nahurst
nahurst / first_match.rb
Created September 28, 2011 21:26
First match with ruby
# stops once it finds a match
@users.detect { |u| u.gender == 'm' }
@nahurst
nahurst / inject.rb
Created July 26, 2011 18:26
Inject example
[1,2,3].inject([]) {|new_array, incoming_item| new_array << "item#{incoming_item}"}
# => ["item1", "item2", "item3"]
@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"]
@nahurst
nahurst / show_queries.rb
Created June 1, 2011 20:02
Show queries in rails console
ActiveRecord::Base.logger = Logger.new(STDOUT)