Skip to content

Instantly share code, notes, and snippets.

View michaelminter's full-sized avatar

Michael Minter michaelminter

View GitHub Profile
=begin
Given 2 water jugs of different sizes and an unlimited source of water, perform different operations to end up with some goal amount of water.
Write out the operations and how much water is in the jugs at each step.
Problem 1 --- Small = 3, Big = 5, Goal = 4
- Start (0, 0)
- Fill small (3, 0)
- pour to big (0, 3)
- Fill small (3, 3)
class BaseQuery
attribute_accessor :object, :query
def initialize
@object = Null
@query = [@object]
end
def execute
@query.map.with_index { |q, i| i == 0 ? q : merge(q) }.join(send '.') # wtf
/*
CONTEXT:
- add a brief description of why we need this query
RESULT EXPECTATION
- add a brief description of your expectations for the query result
ASSUMPTION:
- add assumption about business logic
- add assumption about data
*/
@michaelminter
michaelminter / statement_sql.rb
Created November 13, 2019 18:14
Selecting data with group by comparing arrays
# @param [String] account_id
# @param [String] exclusions
def statement(account_id, exclusions = nil)
<<-STATEMENT
SELECT
JSON_AGG(profiles.id) id,
JSON_AGG(users.external_id) external_id
FROM profiles
LEFT JOIN users ON users.id = profiles.user_id
WHERE
nc -vz google.com 80
# Connection to google.com 80 port [tcp/http] succeeded!
@michaelminter
michaelminter / s3-download.sh
Created September 12, 2019 18:04
Download files from S3 on command line
ssh -fN -L 5433:db.prod:5432 bastion-prod
aws s3 cp s3://<dir>/prod/ . --recursive --exclude "*" --include "201901*"
scope :by_persons, ->(person1, person2) do
arel = Communication.arel_table
up_context1 = arel.grouping(arel[:sender_type].eq(person1.class.name).and(arel[:sender_id].eq(person1.id)))
dn_context1 = arel.grouping(arel[:sender_type].eq(person2.class.name).and(arel[:sender_id].eq(person2.id)))
up_context2 = arel.grouping(arel[:recipient_type].eq(person1.class.name).and(arel[:recipient_id].eq(person1.id)))
dn_context2 = arel.grouping(arel[:recipient_type].eq(person2.class.name).and(arel[:recipient_id].eq(person2.id)))
context1 = arel.grouping(up_context1.or(dn_context1))
context2 = arel.grouping(up_context2.or(dn_context2))
@michaelminter
michaelminter / destructuring.js
Created June 27, 2019 06:19 — forked from mikaelbr/destructuring.js
Complete collection of JavaScript destructuring. Runnable demos and slides about the same topic: http://git.mikaelb.net/presentations/bartjs/destructuring
// === Arrays
var [a, b] = [1, 2];
console.log(a, b);
//=> 1 2
// Use from functions, only select from pattern
var foo = () => [1, 2, 3];
@michaelminter
michaelminter / csv_to_s3.rb
Created April 29, 2019 17:40
Upload CSV stream from Rails to S3
def query_results
ActiveRecord::Base.connection.execute('SELECT name FROM accounts;').to_a
end
# @params [Array<Hash>] records
def generate_csv(records)
attributes = records.first.keys
CSV.generate(headers: true) do |csv|
csv << attributes