Skip to content

Instantly share code, notes, and snippets.

@kalsan
kalsan / method_accessible_hash.rb
Last active March 1, 2023 14:12
MethodAccessibleHash, a possible replacement for OpenStruct
# OpenStruct has caveats and security problems and even leads to a rubocop offense:
# https://ruby-doc.org/stdlib-3.1.0/libdoc/ostruct/rdoc/OpenStruct.html#class-OpenStruct-label-Caveats
# https://msp-greg.github.io/rubocop/RuboCop/Cop/Style/OpenStructUse.html
#
# It's time to replace it. The feature I loved most about OpenStruct was being able to retrieve
# values by calling a method instead of having to use hash access syntax.
# The recommended way to go is Struct, but it is much more cumbersome than OpenStruct was.
# So here's MethodAccessibleHash that allows to do that, but without OpenStruct's security problems.
#
# This revision adds writers, direct creation from Hash, as well as enhanced merging.
@kalsan
kalsan / nginx-location-proxy_pass-and-slashes.txt
Last active October 2, 2020 15:01
Examples demonstrating how Nginx treats slashes in `location` and `proxy_pass` statements
How Nginx treats slashes in `location` and `proxy_pass` statements:
Base URL: https://www.my.web.site/pre/fix/page.html
Our decuced rules, apply for each block A-C:
1: Location is passed on without change
2: Location is removed, '/' is added
3: Location is removed, 'pre/fix' is added
4: Location is removed, 'pre/fix/' is added
@kalsan
kalsan / interruptable-idle.bash
Created June 7, 2020 16:01
A small bash script that does nothing but can be interrupted quickly with SIGTERM. Great as an ENTRYPOINT to keep a Docker container running while developing / debugging.
#!/bin/bash
pid=
trap '[[ $pid ]] && kill "$pid"' EXIT
sleep infinity & pid=$!
wait
pid=
# This file demonstrates that attributes created by class_attribute (in Rails' ActiveSupport) are not thread-safe.
# On my system, the test fails sometimes when setting count to 1_000 and always with a count of 10_000.
# Expected exception: NameError: Undefined method setting for 'BugTest::MyClass'
# Note: Occurs on jruby only.
# See issue https://github.com/rails/rails/issues/26758
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'