Skip to content

Instantly share code, notes, and snippets.

View nbfritz's full-sized avatar

Nathan Fritz nbfritz

  • Spring Hill, TN
View GitHub Profile
@nbfritz
nbfritz / gist:4235810
Created December 7, 2012 19:30
HTTP Status Message Type lookup using ranges as keys for a hash
STATUS_MESSAGE_TYPES = Hash.new {|h,k|
key = h.keys.find {|e| e.class == Range && e.include?(k) }
key ? h[k] = h[key] : :undefined
}.merge({
100..102 => :informational,
200..208 => :success,
226 => :success,
300..308 => :redirection,
400..417 => :client_error,
422..431 => :client_error,
@nbfritz
nbfritz / hash_post_1_a.rb
Created December 14, 2012 14:33
Ruby Hash Post - Part 1: hash creation examples
some_hash = { :some_key => 'a value' } # standard syntax
other_hash = { some_key: 'a value' } # Ruby 1.9 syntax
@nbfritz
nbfritz / hash_post_1_b.rb
Created December 14, 2012 14:39
Ruby Hash Post - Part 1: empty hash creation
empty_hash = {} # using the hash literal syntax
empty_hash = Hash.new # using the Hash class
@nbfritz
nbfritz / hash_post_1_c.rb
Created December 14, 2012 14:57
Ruby Hash Post - Part 1: working counter hash
counter = Hash.new(0) # => {}
counter[:monkey] += 1 # => 1
counter[:monkey] += 2 # => 3
counter # => {:monkey => 3}
@nbfritz
nbfritz / hash_post_1_d.rb
Created December 14, 2012 14:58
Ruby Hash Post - Part 1: explodey hash counter
counter = {} # => {}
counter[:monkey] += 1 # BOOM! NoMethodError: undefined method `+' for nil:NilClass
@nbfritz
nbfritz / hash_post_1_e.rb
Created December 14, 2012 15:27
Ruby Hash Post - Part 1: pretty good status lookup
STATUS_MESSAGES = {
200 => :ok,
201 => :created,
202 => :accepted
}
STATUS_MESSAGES[200] # => :ok
STATUS_MESSAGES[999] # => nil
@nbfritz
nbfritz / hash_post_1_f.rb
Created December 14, 2012 15:28
Ruby Hash Post - Part 1: better message lookup
STATUS_MESSAGES = Hash.new(:undefined).merge({
200 => :ok,
201 => :created,
202 => :accepted
}
STATUS_MESSAGES[200] # => :ok
STATUS_MESSAGES[999] # => :undefined
@nbfritz
nbfritz / hash_post_2_a.rb
Created December 14, 2012 15:36
Ruby Hash Post - Part 2: ranges as keys don't work
STATUS_TYPES = {
200..208 => :success,
500..511 => :server_error
}
STATUS_TYPES[200] # => nil
@nbfritz
nbfritz / hash_post_2_b.rb
Created December 14, 2012 15:37
Ruby Hash Post - Part 2: ranges CAN work as keys
STATUS_TYPES = Hash.new {|this_hash,missing_key|
found_key = this_hash.keys.find {|this_key| this_key.class == Range && this_key.include?(missing_key) }
found_key ? this_hash[missing_key] = this_hash[found_key] : :undefined
}.merge({
200..208 => :success,
500..511 => :server_error
})
STATUS_TYPES.keys # => [200..208, 500..511]
STATUS_TYPES[200] # => :success
# settings {{{
config defaultToCurrentScreen true
config nudgePercentOf screenSize
config resizePercentOf screenSize
alias gridSize 8,8
alias c (screenSizeX/8)
alias r (screenSizeY/8)
#}}}