Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / expand.rb
Created February 12, 2012 02:18
Expand a sequence to an array
[*1..5] # => [1, 2, 3, 4, 5]
@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 / 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 / latency.txt
Created September 13, 2012 01:34 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers Time Light Distance Approximate Light Distance
-------------------------- ---- -------------- --------------------------
L1 cache reference 0.5 ns 0.15 m Diagonal across your smartphone
Branch mispredict 5 ns 1.5 m Height of Natalie Portman
L2 cache reference 7 ns 2.1 m Height of Shaq
Mutex lock/unlock 25 ns 7.5 m Height of a school flag pole
Main memory reference 100 ns 30 m Half a Manhattan city block (North/South)
Compress 1K bytes with Zippy 3,000 ns 900 m Width of Central Park
Send 1K bytes over 1 Gbps network 10,000 ns 3,000 m Width of Manhattan
Read 4K randomly from SSD* 150,000 ns 45,000 m NYC to Hempstead on Long Island
@nahurst
nahurst / console_spec.rb
Created October 14, 2012 20:52
Using pry like a rails console that connects to spork to speed up initialization
describe "console" do
it "should run" do
binding.pry
end
end
#> spork
#> rspec --drb spec/models/console_spec.rb