Skip to content

Instantly share code, notes, and snippets.

@mrk21
mrk21 / code
Last active November 26, 2021 05:10
This `code` command is for containers, and it can also execute on terminals other than vscode
#!/bin/bash
# This `code` command is for containers, and it can also execute on terminals other than vscode.
# If you use it, you must execute it after attaching the container in vscode.
# see: VSCode Remote SSHで別Shellからファイルを開く https://zenn.dev/shirou/articles/vscode-remote-open-file
export VSCODE_IPC_HOOK_CLI=$(ls -t /tmp/vscode-ipc-*.sock | head -1)
CODE=$(ls -t ~/.vscode-server/bin/*/bin/code | head -1)
exec ${CODE} $*
#include<type_traits>
#include<utility>
#include<iostream>
template<typename Callable>
union storage
{
storage() {}
std::decay_t<Callable> callable;
};
@mrk21
mrk21 / crypto.js
Last active June 5, 2020 21:43
HMAC SHA256 digest of CryptoJS does not match it of OpenSSL, so should use jsSHA instead of CryptoJS.
const cryptoJsHmacSHA256Digest = (key, data) => {
const hmac = CryptoJS.HmacSHA256(data, key);
const buffer = new ArrayBuffer(hmac.sigBytes);
const dv = new DataView(buffer);
hmac.words.forEach((v, i) => dv.setInt32(4*i, v, false));
let digest = '';
for (let i = 0; i < hmac.sigBytes; i++) {
digest += String.fromCharCode(dv.getUint8(i));
}
@mrk21
mrk21 / binary-to-string.js
Created June 5, 2020 21:09
Binary to Binary String
const data = [ 102222, 4000032 ];
const buffer = new ArrayBuffer(4 * 2);
const dv = new DataView(buffer);
data.forEach((v, i) => dv.setInt32(4 * i, v, false));
let result = '';
for (let i = 0; i < 4 * 2; i++) {
result += String.fromCharCode(dv.getUint8(i));
}
console.log(btoa(result))
@mrk21
mrk21 / tailcall_optimization.rb
Created May 21, 2020 06:28
Tail call optimization for Ruby
# @see [Rubyの末尾呼び出し最適化を試す - Qiita](https://qiita.com/rsnni/items/8f818f2c2da07896fc7e)
RubyVM::InstructionSequence.compile_option = {
:tailcall_optimization => true,
:trace_instruction => false
}
RubyVM::InstructionSequence.new(<<-EOS).eval
def fact(x)
return 1 if x == 0
@mrk21
mrk21 / redis_lock.rb
Created January 24, 2020 15:10
Locking between multiple Ruby processes by Redis
class Lock
def initialize(redis:, key:)
@redis = redis
@key = key
end
def lock(timeout_sec: 10)
start = Time.now.to_i
loop do
break yield if @redis.setnx(@key, true)
@mrk21
mrk21 / singleton_method_definition.rb
Created January 22, 2020 05:08
Local variables accessible singleton method definition for Ruby
class Hoge
def a
1
end
end
hoge = Hoge.new
b = 2
hoge.define_singleton_method(:b){ b }
@mrk21
mrk21 / sleep.js
Created December 10, 2019 11:13
Sleep for JavaScript (>= ES2015)
const sleep = (msec) => new Promise(resolve => setTimeout(resolve, msec));
const main = async () => {
console.log(1);
await sleep(1000);
console.log(2);
await sleep(1000);
console.log(3);
await sleep(1000);
};
@mrk21
mrk21 / app_controllers_hoges_controller.rb
Created October 3, 2019 09:43
Validation on destroying for Rails
# frozen_string_literal: true
class HogesController < ApplicationController
def destroy
hoge = Hoge.find(params[:id])
hoge.destroy!
redirect_to hoges_path
rescue ActiveRecord::RecordNotDestroyed => e
flash[:error] = e.record.errors.full_messages.join("\n")
flash[:error] = 'Destroying failed' if flash[:error].blank?
@mrk21
mrk21 / output_log_for_net_http.rb
Created September 25, 2019 06:00
How to output logs for Net::HTTP
require 'net/http'
# @see https://qiita.com/mechamogera/items/d9ea1cf043e976641a23#rails%E3%81%A7%E7%A2%BA%E8%AA%8D%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95
class Net::HTTP
alias :create :initialize
def initialize(*args)
logger = ActiveSupport::Logger.new(STDOUT)
create(*args)
self.set_debug_output logger