-- Temporal.
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
# frozen_string_literal: true | |
RSpec.describe TestNPlusOnes do | |
describe "<something>" do | |
subject(:generate) { puts("<Do something with the DB>") } | |
# Technique seen in ActiveRecord's #assert_queries_count to use with Minitest: | |
# https://github.com/rails/rails/blob/0496a5f994f134695bf7bdc02dacdf24925bc67c/activerecord/lib/active_record/testing/query_assertions.rb#L99 | |
it "avoids N+1 queries" do | |
number_of_queries = 0 | |
counter = ->(*, payload) { |
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
/* | |
USAGE EXAMPLES: | |
Is the tenth row hidden? | |
> isRowHidden(sheet.getRange('B10')) | |
Is column B hidden? | |
> isColumnHidden(sheet.getRange('B10')) | |
Is cell B10 visible? (not in a hidden row and/or column) |
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
-- WINDOW EXAMPLE: | |
SELECT id | |
, col1 | |
, col2 | |
, bool_col | |
, datecuen | |
, (CASE WHEN bool_col IS NOT NULL | |
THEN SUM(col1) OVER win1 | |
+ SUM(col2) OVER win1 | |
ELSE 0 |
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
# LINK: https://en.wikipedia.org/wiki/Pairing_function#Cantor_pairing_function | |
# LINK: https://www.cantorsparadise.com/cantor-pairing-function-e213a8a89c2b | |
def inv_cantor(z) | |
w = ((Math.sqrt(8.0*z.to_f + 1.0) - 1.0) / 2.0).floor | |
t = (w**2 + w) / 2 | |
y = (z - t) | |
x = (w - y) | |
[x, y] | |
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 'json' | |
class Person | |
attr_accessor :name, :age | |
def initialize(name, age) | |
@name = name | |
@age = age | |
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
# USAGE: | |
# ------ | |
# mongo_id_from_date Date.parse('2020-12-24') # => "5fe3da000000000000000000" | |
# mongo_id_from_date(Time.now) # => "5fe471d90000000000000000" | |
def mongo_id_from_date(date) | |
min_date = Time.at 0 | |
max_date = Time.at 0xffffffff | |
raise "Error: date must be between #{min_date.to_date} and #{max_date.to_date}" if date < min_date || date > max_date |
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
#!/usr/bin/env bash | |
echo 'STATIC-CHECKING RUBY SYNTAX OF ALL RB & RAKE FILES IN PROJECT' | |
echo | |
find . | grep ".*\.rb$" | xargs -L 1 ruby -c | |
find . | grep ".*\.rake$" | xargs -L 1 ruby -c |
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
# frozen_string_literal: true | |
require 'base64' | |
def number_to_base64(number, base: nil) | |
valid_base = ->(b) { | |
b.is_a?(Integer) && b > 1 | |
} | |
hex2int = ->(hex_chars) { | |
hex_chars.to_i(16) |
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
# Usage: | |
# my_struct = { alpha: 1, beta: 2, 'charlie' => 3 }.to_struct | |
# my_struct.alpha # => 1 | |
# my_struct.beta # => 2 | |
# my_struct.charlie # => 3 | |
# Remember, to compare structs: `my_struct.to_h == other_struct.to_h` | |
# | |
# Usage to get deep structs: | |
# my_deep_struct = { | |
# email: 'alice@example.com', |
NewerOlder