This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "active_record" | |
require 'mysqlplus' | |
Mysql.class_eval { alias :query :async_query } | |
ActiveRecord::Base.establish_connection( | |
:adapter => "mysql", | |
:username => "root", | |
:database => "test", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run lambda { |env| [302, {'Content-Type'=>'text/plain', 'location' => 'http://blog.shrewple.com'}, Array.new] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while self[:slug].nil? && Referral.exists? :slug => self[:slug] | |
self[:slug] = ActiveSupport::SecureRandom.hex 6 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
def r b; b*38 if URI('http://roulette.engineyard.com/').read[/13/] end |
OlderNewer