Skip to content

Instantly share code, notes, and snippets.

Avatar
🛠️
Building @tellerhq

Stevie Graham stevegraham

🛠️
Building @tellerhq
View GitHub Profile
View Application Configuration Class
class AppConfig
# Loads a YAML configuration file from RAILS_ROOT/config/. The default file
# it looks for is 'application.yml', although if this doesn't match your
# application, you can pass in an alternative value as an argument
# to AppConfig.load.
# After the file has been loaded, any inline ERB is evaluated and unserialized
# into a hash. For each key-value pair in the hash, class getter and setter methods
# are defined i.e., AppConfig.key => "value"
# This allows you to store your application configuration information e.g., API keys and
# authentication credentials in a convenient manner, external to your application source
View gist:319658
class Ability
include CanCan::Ability
def initialize(user)
return if user.nil?
# Collect rights from roles
authorizations = HashWithIndifferentAccess.new []
user.roles.each do |role|
role.resource_rights.each { |resource,actions| authorizations[resource] |= actions }
View gist:343487
# Run this file in the same directory as weather.dat
class NOAAParser
def initialize(file)
# Reject all lines in file that don't begin with whitespace and integer
@lines = IO.readlines(file).select { |line| line =~ /^\s+\d/ }
end
def day_with_lowest_temp_spread
# Maybe this regexp could be cleaner?
View gist:345141
[NilClass, FalseClass, TrueClass, Symbol, Numeric, Class, Module].each { |klass| klass.class_eval { def duplicable?() false end } }
# Or even…
[NilClass, FalseClass, TrueClass, Symbol, Numeric, Class, Module].each do |klass|
klass.class_eval { def duplicable?() false end }
end
View gist:351374
require "rubygems"
require "active_record"
require 'mysqlplus'
Mysql.class_eval { alias :query :async_query }
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:username => "root",
:database => "test",
View gist:479087
%w(rubygems eventmachine em-http-request yajl).each { |lib| require lib }
module Twitter
def on_tweet() lambda { |obj| puts obj['text'] }; end
extend self
end
EM::run do
req = EM::HttpRequest.new('http://stream.twitter.com/1/statuses/sample.json').get :head => { 'authorization' => ['eventmachine', 'eventm4chine'] }
parser = Yajl::Parser.new.tap { |p| p.on_parse_complete = Twitter.on_tweet }
View config.ru
run lambda { |env| [302, {'Content-Type'=>'text/plain', 'location' => 'http://blog.shrewple.com'}, Array.new] }
View gist:662917
def method_missing(meth, *args, &blk)
if value.include? meth
class << self; self; end.instance_eval { define_method(meth) { value[meth] } }
send meth
else
super
end
end
View gist:667896
while self[:slug].nil? && Referral.exists? :slug => self[:slug]
self[:slug] = ActiveSupport::SecureRandom.hex 6
end
View roulette.rb
require 'open-uri'
def r b; b*38 if URI('http://roulette.engineyard.com/').read[/13/] end