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 'debug_log' | |
my_list = [:a, :b, :c] | |
my_number = 5 | |
debug.log("after variable initialization", "my_number", "my_list") | |
# => DebugLog | after variable initialization | my_number="5" (Fixnum), my_list="[:a, :b, :c]" (Array) | /Users/peter/tmp/debug-example.rb:6:in `foo' |
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 ImportLegacyDevices < ActiveRecord::Migration | |
def self.up | |
return unless Rails.env.production? | |
legacy_devices.each do |device_id, issue| | |
if device = Device.find_by_hardware_id(device_id) | |
unless InclusiveIssue.exists?(:issue_id => issue.id, :device_id => device.id) | |
InclusiveIssue.create!( | |
:issue => issue, | |
:device => device, |
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
module NullifyTextAttributes | |
def self.included(base) | |
base.class_eval do | |
before_validation :nullify_text_attributes | |
private | |
def nullify_text_attributes | |
self.class.columns.select { |c| [:text, :string].include?(c.type) }.each do |column| | |
send("#{column.name}=", nil) if read_attribute(column.name).blank? | |
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
# Traditional idiom | |
total = 1 | |
[2, 3, 5].each do |value| | |
total = total * value | |
end | |
puts total | |
# Ruby idiom | |
puts [2, 3, 5].inject { |mult, value| mult * value } |
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 counts_from_dice(dice) | |
dice.inject({}) do |hash, value| | |
hash[value] ||= 0 | |
hash[value] += 1 | |
hash | |
end | |
end | |
def score(dice) | |
counts_from_dice(dice).inject(0) do |sum, (number, count)| |
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
# 0. Make sure you have Ruby 1.9.3 installed, and optionally RVM and PostgreSQL | |
# 0.2 If you are on the Mac, make sure you have a c compiler by installing XCode Command Line Tools or gcc4.2 with homebrew | |
# https://github.com/mxcl/homebrew/wiki/Custom-GCC-and-cross-compilers | |
# 0.5 Make sure you have bundler version ~> 1.2 as Rails depends on it | |
gem install bundler | |
# 1. Get edge Rails source (master branch) | |
git clone https://github.com/rails/rails.git |
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
# Poor man's identity map for reusing ActiveRecord objects and avoid fetching the same | |
# record multiple times from the database within a request or background job. The check method can | |
# be used to locate duplicate queries in development. The goal is to instantiate | |
# each ActiveRecord object only once. The Rails identity map is known to have issues: | |
# http://stackoverflow.com/questions/6905891/rails-3-1-identity-map-issues | |
class IdentityMap | |
# Set one or more ActiveRecord objects in the map | |
def set(*objects) | |
objects.each do |object| | |
map[key(object)] = object |
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
################################################################# | |
# | |
# File: lib/api.rb | |
# | |
################################################################# | |
require 'user' # ActiveRecord model | |
# Connect to the db | |
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL']) |
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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
// Hooking up the middleware with the express app | |
// In app.js | |
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
var forceSSL = require('../middleware/ssl').force(config.hostname); | |
if ('production' == app.get('env')) { | |
app.use(forceSSL); | |
} |
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
# NOTE: There must be a better way to do this - help appreciated! | |
############################################################### | |
# uploaded_file.rb - utility class | |
############################################################### | |
class UploadedFile | |
def self.size(file) | |
uploaded?(file) ? file.size : nil | |
end |