Skip to content

Instantly share code, notes, and snippets.

View leonid-shevtsov's full-sized avatar
🇺🇦
Help Ukraine Win

Leonid Shevtsov leonid-shevtsov

🇺🇦
Help Ukraine Win
View GitHub Profile
@leonid-shevtsov
leonid-shevtsov / monitor_online_status.ts
Created December 3, 2022 21:35
Автоматично повідомляй команду про наявність звʼязку за допомогою Firebase
// 1. Set up Firebase app
// 2. Set up Slack webhook
// 3. Configure slack.webhook_url in the app
// 4. Deploy functions
// 5. Note the `canary` function URL
// 6. Make sure to GET the `canary` endpoint once an hour.
// 7. That's all!
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
require 'benchmark/ips'
def sum(x, y)
x + y
end
def nested_sum(x, y)
sum(x, y)
end
@leonid-shevtsov
leonid-shevtsov / fish_prompt.fish
Last active September 3, 2022 12:45
Minimal fish prompt for VSCode
function fish_prompt
if not set -q VSCODE_WS; or test $VSCODE_WS = '${workspaceFolder}'
# not vscode or no active workspace
set shortpath (prompt_pwd)
else
set shortpath (realpath --relative-to $VSCODE_WS .)
if test (string sub --length 2 $shortpath) = ".."
# working dir is outside of workspace
set shortpath (printf "🌍 %s" (prompt_pwd))
else if test $shortpath = '.'
@leonid-shevtsov
leonid-shevtsov / sqs.hsl
Created May 27, 2020 06:05
Send data to AWS SQS (Simple Queue Service) from Halon MTA (with signature)
function signedHeaders($headers) {
return array_join(array_sort(function ($a, $b) { return $a < $b; }, array_map(function($key) { return str_lower($key); }, array_keys($headers))), ";");
}
function canonicalRequest($method, $uriPath, $queryString, $headers, $signedHeaders, $requestBody) {
$headersString = array_join(array_sort(function ($a, $b) { return $a < $b; }, array_map(function($key) closure($headers) { return str_lower($key) . ":" . $headers[$key]. "\n"; }, array_keys($headers))));
$payloadHash = sha2($requestBody, 256);
return $method . "\n" . $uriPath . "\n" . $queryString . "\n" . $headersString . "\n" . $signedHeaders . "\n" . $payloadHash;
}
<?
// расчет числа рабочих дней в промежутке дат
// $start_day = 1 марта 2020, $total_days = 12
function workday_count($start_day_as_timestamp,$total_days)
{
//$weekday 0 = Sun 1 = Mon ...
$weekday = date('w',$start_day_as_timestamp);
// $weekday = 0: 1 марта 2020 - воскресенье
import * as React from 'react';
export type ToggleChildProps = {
toggle: () => void;
};
type WithToggleChildProps<P extends object> = ToggleChildProps &
Pick<P, Exclude<keyof P, 'viewComponent' | 'editComponent'>>;
type ToggleProps<P extends object> = {
require 'set'
require 'benchmark/ips'
Benchmark.ips do |x|
array = (1..50000).to_a
shuffled_array = array.shuffle
set = array.to_set
hash = array.zip([false]*50000).to_h
x.report("with array") { array.include?(Random.rand(50000)) }
x.report("with shuffled array") { shuffled_array.include?(Random.rand(50000)) }
package main
import (
"fmt"
"math/rand"
"time"
)
func sortedChan(inputChans []<-chan int) <-chan int {
outChan := make(chan int)

Code that does not run is code that rots

In Ruby, there is absolutely no guarantee that a method's declared signature will match the call signature. The only practical way to ensure this is to execute the call.

Thus, if you have code that is not covered by unit tests, you rely on manual testing - and production - to check that it even links, let alone does what you expect.

From my experience with explicit typing, it eliminates a huge class of refactoring mistakes, where you forget to update a method or a call signature. In Ruby, instead of explicit typing, we have unit tests.

This, to me, is the big reason to write unit tests in Ruby - not because it's more productive, or makes for better code. Code that verifiably links is reason enough to write them.

#!/bin/bash
# Put in $PATH/git-pr; chmod +x
# Call with `git pr` to open a compare view for the current branch and a pull request from that
# Depends on https://hub.github.com/
hub compare dev..`git rev-parse --abbrev-ref HEAD`