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 / 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
@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 / 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 / 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 / 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 / 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 / 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 / boxee.js
Created March 24, 2012 17:50
boxee.js
boxee.apiMinVersion=7.0;
boxee.reloadOnPageChange = true;
boxee.setMode(boxee.CURSOR_MODE);
boxee.onKeyboardKeyDown = function() {
document.write("TIEG")
}
boxee.onMouseMoveDown = function() {
document.write("MOUSE MOVE DOWN");
}
@tiegz
tiegz / class_inheritable_attribute_pattern.rb
Created March 28, 2012 15:07
Pattern for class inheritable_attributes that was removed from rails 3.2
class SomeClass
self.instance_variable_set('@my_attribute', {:class => true})
def self.my_attribute; @my_attribute; end
def self.inherited(klass)
val = self.instance_variable_get('@my_attribute')
klass.instance_variable_set('@my_attribute', val.duplicable? ? val.dup : val)
super
end
end
@tiegz
tiegz / class_attribute_behavior.rb
Created March 28, 2012 15:29
The behavior of superclasses and classes with Rails 3.2 class_attribute
// Key difference: the old way of using {write,read}_inheritable_attribute was
// a *static default* for child classes. The new way of using class_attribute is a
// *dynamic default*.
class A
class_attribute :foo
self.foo = 1
end
class B < A; end
class C < B; end