Skip to content

Instantly share code, notes, and snippets.

class Array
def frequencies(&block)
block ||= ->(x) {x}
Hash[group_by(&block).map {|k,v| [k, v.present? ? v.count : nil]} ]
end
end
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, '"$&"'))
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)
# #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(//)
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
# A sample Gemfile
source "https://rubygems.org"
# gem "rails"
gem 'haml'
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]
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
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
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]