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 Array | |
def frequencies(&block) | |
block ||= ->(x) {x} | |
Hash[group_by(&block).map {|k,v| [k, v.present? ? v.count : nil]} ] | |
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
assert = require 'assert' | |
_ = require 'lodash' | |
globals = | |
'+': (a,b) -> a + b | |
'*': (a,b) -> a * b | |
parse = (input) -> | |
JSON.parse(input.replace(/\s/g,",").replace(/[a-z\+\*]+/g, '"$&"')) |
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
fns = | |
'+': (a,b) -> a + b | |
'*': (a,b) -> a * b | |
parse = (input) -> | |
JSON.parse(input.replace(/\s/g,",").replace(/\+|\*/g, '"$&"')) | |
evaluate = (x)-> | |
if (Array.isArray(x)) # x is an expression | |
results = x.map (expression) -> evaluate(expression) |
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
# #fire | |
# Takes: r (A-J),c (1-10) | |
# Returns: MISS, HIT, SUNK, WIN, ()DUPE) | |
# #place | |
# Takes: r (A-J),c (1-10), Ship object | |
# Returns success/failure as a boolean | |
class Board | |
ROWS = 'ABCDEFGHIJ'.split(//) |
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 SendAll | |
def send_all(methods_and_args) | |
return self if methods_and_args.empty? | |
method_name, args = methods_and_args.pop | |
result = send method_name, *args | |
result.send_all result, query | |
end | |
end | |
class ActiveRecord::Relation |
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
# A sample Gemfile | |
source "https://rubygems.org" | |
# gem "rails" | |
gem 'haml' |
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 ActiveRecord::Base | |
descendants.each do |klass| | |
klass.singleton_class.instance_eval do | |
attr_accessor :touches, :touched_by | |
end | |
klass.touches = [] | |
klass.touched_by = [] | |
klass.reflections.values.each do |reflection| | |
if reflection.options && reflection.options[:touch] |
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 Profiler | |
self.singleton_class.instance_eval do | |
attr_accessor :defaults | |
end | |
self.defaults = { | |
open: true, | |
path: 'tmp', | |
name: 'profile' | |
} | |
def self.profile options = {}, &block |
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 Profiler | |
self.singleton_class.instance_eval do | |
attr_accessor :defaults | |
end | |
self.defaults = { | |
open: true, | |
path: 'tmp', | |
name: 'profile' | |
} | |
def self.profile options = {}, &block |
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 Profiling | |
class CallStackTracker | |
cattr_accessor :query_call_stacks do | |
[] | |
end | |
IGNORED_SQL = [/^PRAGMA (?!(table_info))/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/] | |
def call(name, start, finish, message_id, values) | |
# FIXME: this seems bad. we should probably have a better way to indicate | |
# the query was cached | |
unless 'CACHE' == values[:name] |
NewerOlder