Skip to content

Instantly share code, notes, and snippets.

View tiegz's full-sized avatar
👋
Working from home

Tieg Zaharia tiegz

👋
Working from home
View GitHub Profile
@tiegz
tiegz / rails3_vs_grape_apis.rb
Created January 25, 2012 23:17
Rails 3 vs Grape benchmark
# config/routes.rb
mount MyGrapeAPI => "/grape_api"
namespace :api do
namespace :v1 do
resources :projects
end
end
#######################
##### RAILS 3 API #####
@tiegz
tiegz / swimming.rb
Created September 15, 2011 22:39
swimming in bash
module Kernel
@@frame1 = " ======== ᙅ ⎌ ᙂ ========"
@@frame2 = " ======== ᘩ ⎌ ᘵ ========"
@@framepos = 1
def swim
frame = @@framepos == 1 ? @@frame1 : @@frame2
print frame
sleep 0.3
print "\b" * frame.size
@tiegz
tiegz / Rails3AuthlogicReorder.rb
Created August 17, 2011 18:19
Prioritize order of Authlogic::Session::Base persistence callbacks for Rails 3
class UserSession < Authlogic::Session::Base
... # rest of your code
_persist_callbacks.replace(_persist_callbacks.sort_by { |cb|
order = %w(params session http_auth cookie).index(cb.filter.to_s.sub(/^persist_by_/, ''))
order || _persist_callbacks.size
})
__define_runner(:persist) # must regenerate _run_persist_callbacks() to take effect
end
@tiegz
tiegz / Rails2AuthlogicReorder.rb
Created August 2, 2011 19:55
Prioritize order of Authlogic::Session::Base persistence callbacks for Rails 2
class UserSession < Authlogic::Session::Base
... # rest of your code
private
# Reorder persist_callback_chain at runtime (only way I know of doing this).
def run_callbacks(kind, options = {}, &block)
self.class.send("#{kind}_callback_chain").tap { |callback_chain|
if kind.to_s == "persist"
@tiegz
tiegz / gist:1085543
Created July 15, 2011 21:03
Paste this into browser bar in Chrome (abstracted from http://html5-demos.appspot.com/static/html5-whats-new/template/index.html)
We couldn’t find that file to show.
@tiegz
tiegz / bcryptness.rb
Created June 15, 2011 22:56
bcryptness
require "bcrypt"
a = ::BCrypt::Password.create "somepassword" # => "$2a$10$DaVOCJ1GX1z9ZPMAda2UQOwyZQKbghWwHc249YURGlGhsfNy/PZtm"
b = ::BCrypt::Password.create "somepassword" # => "$2a$10$BA6xc2/2WQ30T.rHsviMH.SP8/9JXbRtvgABceoNzd8z2.xO9J1IC"
::BCrypt::Password.new(a) == "somepassword" # => true
::BCrypt::Password.new(b) == "somepassword" # => true
@tiegz
tiegz / shell_spinner.rb
Created June 1, 2011 21:39
A ruby helper for printing out ongoing progess (for shell).
module Kernel
# Prints out progress
@@printp_spinner = %w(⇐ ⇖ ⇑ ⇗ ⇒ ⇘ ⇓ ⇙)
@@printp_spinnerpos = 0
@@printp_cols = [`tput cols`.to_i - 20, 50].max
def printp(symbol, i)
print @@printp_spinnerpos % @@printp_cols == 0 ? "\n" : ("\b" * (16 + symbol.size))
print "#{symbol}#{" %3d%" % i} complete #{@@printp_spinner[(@@printp_spinnerpos+=1) % 8]} "
STDOUT.flush
end
puts a = (2500 * 56) / 10000.0 # => 14.0
puts b = (250 * 5.6) / 100.0 # => 14.0
puts c = 25 * 0.56 # => 14.0
puts a % 1 # => 0 ... Expected
puts b % 1 # => 0 ... Expected
puts c % 1 # => 1.77635683940025e-15 .. Not Expected
@tiegz
tiegz / gist:944349
Created April 27, 2011 14:36
Ruby 1.9 Named Regexp Groups
if /(?<hour>\d?\d)\:(?<min>\d\d)(?<suffix>[pa]m)/ =~ "5:03pm"
puts hour
puts min
puts suffix
end
@tiegz
tiegz / gist:920550
Created April 14, 2011 21:05
a will_paginate extension
module WillPaginate
class Collection
# This is a special case of paginate() for _dubstep_ fans (_hyper-dubstep_ not supported).
# It is different in this regard:
# * The first page should load +first_per_page+ records
# * The rest of the pages should load +more_per_page+ records
#
# NOTE: will_paginate view helpers not really tested with this (eg the '1,2,3...N' thing)
#
def self.dubstep_paginate(finder, page = 1, first_per_page = 15, more_per_page = 15, finder_opts = {})