Skip to content

Instantly share code, notes, and snippets.

@romanbsd
romanbsd / stringio.cpp
Created January 27, 2025 09:35
stringio.cpp
#include <ruby.h>
#include <sstream>
#include <string>
class StringIO {
private:
std::ostringstream oss;
public:
StringIO() = default;
@romanbsd
romanbsd / bitset_to_vector.cpp
Created December 23, 2024 14:35
Convert bitset to vector<T>
template <typename T, size_t BitsetSize>
std::vector<T> bitsetToVector(const std::bitset<BitsetSize>& bitset, size_t bitsPerElement = sizeof(T) * 8) {
static_assert(std::is_unsigned<T>::value, "Output vector type must be an unsigned integer type.");
static_assert(BitsetSize % (sizeof(T) * 8) == 0, "Bitset size must be a multiple of the element size.");
size_t numElements = BitsetSize / bitsPerElement; // Calculate number of elements in the output vector
std::vector<T> result(numElements);
for (size_t i = 0; i < numElements; ++i) {
T value = 0;
# Shim for Rails 6.1 to ease upgrading to 7.1
return if defined? ActiveSupport::BroadcastLogger
module ActiveSupport
class BroadcastLogger < SimpleDelegator
def initialize(*loggers)
logger = loggers.shift
super(logger)
loggers.each { |l| logger.extend(ActiveSupport::Logger.broadcast(l)) }
end
#!/usr/bin/env python3
import os
def generate_header_file(input_file):
# Read binary data from input file
with open(input_file, "rb") as f:
data = f.read()
# Create header file name
@romanbsd
romanbsd / datetime_controller.js
Created June 16, 2024 08:29
Timepicker controller for bootstrap
import { Controller } from '@hotwired/stimulus'
import { TempusDominus } from '@eonasdan/tempus-dominus'
// Connects to data-controller="datetime"
export default class extends Controller {
connect() {
this.element.classList.add('bg-white')
this.element.setAttribute('readonly', true)
this.widget = new TempusDominus(this.element, {
#!/usr/bin/env ruby
require 'digest'
all = Hash.new
Dir["*.jpg"].each do |file|
md5 = Digest::MD5.hexdigest(IO.read(file))
ary = all.fetch(md5, [])
ary << file
all[md5] = ary
def sha512_base64(string)
# Compute SHA-512 digest in binary form
digest = OpenSSL::Digest::SHA512.digest(string)
# Encode the binary digest in base64
base64_encoded = Base64.strict_encode64(digest)
"sha512-#{base64_encoded}"
end
import Chart from 'stimulus-chartjs'
// Connects to data-controller="async-chart"
export default class extends Chart {
static values = {
url: String
}
connect() {
fetch(this.urlValue)
module ExternalApiRuntime
extend ActiveSupport::Concern
module ClassMethods
def log_process_action(payload)
messages = super
api_runtime = payload[:api_runtime]
messages << (format('ExternalAPI: %.1fms', api_runtime.to_f)) if api_runtime
messages
end
import { Controller } from '@hotwired/stimulus'
import { TempusDominus } from '@eonasdan/tempus-dominus'
function findParentBySelector(element, selector) {
while (element && element.parentElement) {
if (element.parentElement.matches(selector)) {
return element.parentElement;
}
element = element.parentElement;
}