Skip to content

Instantly share code, notes, and snippets.

View czarneckid's full-sized avatar

David Czarnecki czarneckid

View GitHub Profile
* We have 3 files that are modified in our local git repository, but we only want to stash 2 of those files.
* If we use git stash save, git will save all 3 files in the stash. How can we stash only README and TODO?
fossil:gitstash dczarnecki$ git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'thin', '1.2.7'
gem 'will_paginate'
gem 'nokogiri'
gem 'haml'
Define a constant if not defined:
define_if_not_defined(:A, 1)
assert_equal 1, A
Define a constant and redefine it:
define_if_not_defined(:B, 1)
redefine_without_warning(:B, 2)
@czarneckid
czarneckid / gist:791760
Created January 23, 2011 02:44
Performance Metrics
10 million sequential scores insert:
ruby-1.8.7-p302 > insert_time = Benchmark.measure do
ruby-1.8.7-p302 > 1.upto(10000000) do |index|
ruby-1.8.7-p302 > highscore_lb.add_member("member_#{index}", index)
ruby-1.8.7-p302 ?> end
ruby-1.8.7-p302 ?> end
=> #<Benchmark::Tms:0x101605660 @label="", @stime=173.61, @total=577.52, @real=911.718175172806, @utime=403.91, @cstime=0.0, @cutime=0.0>
Average time to request an arbitrary page from the leaderboard:
@czarneckid
czarneckid / gist:792239
Created January 23, 2011 17:23
Scenario 1
fossil:leaderboard dczarnecki$ irb
ruby-1.8.7-p302 > require 'rubygems'
=> true
ruby-1.8.7-p302 > require 'leaderboard'
=> true
ruby-1.8.7-p302 > highscore_lb = Leaderboard.new('highscores')
=> #<Leaderboard:0x1011b23b0 @leaderboard_name="highscores", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.4)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}>
ruby-1.8.7-p302 > 1.upto(10) do |index|
ruby-1.8.7-p302 > highscore_lb.add_member("member_#{index}", index)
ruby-1.8.7-p302 ?> end
@czarneckid
czarneckid / gist:792244
Created January 23, 2011 17:27
Scenario 2
ruby-1.8.7-p302 > highscore_lb.add_member('DavidCzarnecki', 15)
=> false
ruby-1.8.7-p302 > highscore_lb.page_size = 5
=> 5
ruby-1.8.7-p302 > highscore_lb.around_me('DavidCzarnecki')
=> [{:member=>"DavidCzarnecki", :score=>"15", :rank=>1}, {:member=>"member_10", :score=>"10", :rank=>2}, {:member=>"member_9", :score=>"9", :rank=>3}, {:member=>"member_8", :score=>"8", :rank=>4}, {:member=>"member_7", :score=>"7", :rank=>5}]
ruby-1.8.7-p302 >
@czarneckid
czarneckid / gist:791756
Created January 23, 2011 02:39
Leaderboard Usage
Create a new leaderboard or attach to an existing leaderboard named 'highscores':
ruby-1.8.7-p302 > highscore_lb = Leaderboard.new('highscores')
=> #<Leaderboard:0x1018e4250 @page_size=25, @port=6379, @host="localhost", @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.1.10)>, @leaderboard_name="highscores">
If you need to pass in options for Redis, you can do this with the redis_options parameter:
redis_options = {:host => 'localhost', :port => 6379, :password => 'password', :db => 'some_redis_db'}
highscore_lb = Leaderboard.new('highscores', redis_options[:host], redis_options[:port], Leaderboard::DEFAULT_PAGE_SIZE, redis_options))
@czarneckid
czarneckid / gist:792290
Created January 23, 2011 18:11
Scenario 4
ruby-1.8.7-p302 > total_money_lb.change_score_for('DavidCzarnecki', -8)
=> "7"
ruby-1.8.7-p302 > total_money_lb.change_score_for('ChristianArca', 16)
=> "23"
ruby-1.8.7-p302 > total_money_lb.leaders(1)
=> [{:member=>"ChristianArca", :score=>"23", :rank=>1}, {:member=>"DavidCzarnecki", :score=>"7", :rank=>2}]
ruby-1.8.7-p302 >
@czarneckid
czarneckid / gist:792283
Created January 23, 2011 18:03
Scenario 3
ruby-1.8.7-p302 > total_money_lb = Leaderboard.new('total_money')
=> #<Leaderboard:0x101180680 @leaderboard_name="total_money", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.4)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}>
ruby-1.8.7-p302 > total_money_lb.change_score_for('DavidCzarnecki', 15)
=> "15"
ruby-1.8.7-p302 > total_money_lb.change_score_for('ChristianArca', 7)
=> "7"
ruby-1.8.7-p302 > total_money_lb.leaders(1)
=> [{:member=>"DavidCzarnecki", :score=>"15", :rank=>1}, {:member=>"ChristianArca", :score=>"7", :rank=>2}]
ruby-1.8.7-p302 >
ruby-1.8.7-p302 > map_1_xp_lb = Leaderboard.new('map_1_xp')
=> #<Leaderboard:0x1018d96e8 @leaderboard_name="map_1_xp", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.3)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}>
ruby-1.8.7-p302 > map_2_xp_lb = Leaderboard.new('map_2_xp')
=> #<Leaderboard:0x1018d34f0 @leaderboard_name="map_2_xp", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.3)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}>
ruby-1.8.7-p302 > map_3_xp_lb = Leaderboard.new('map_3_xp')
=> #<Leaderboard:0x1018cd370 @leaderboard_name="map_3_xp", @page_size=25, @port=6379, @redis_connection=#<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.0.3)>, @host="localhost", @redis_options={:host=>"localhost", :port=>6379}>
ruby-1.8.7-p302 > map_4_xp_lb = Leaderboard.new('map_4_xp')
=> #<Leaderboard:0x1018c71f