Skip to content

Instantly share code, notes, and snippets.

@skojin
skojin / heap_dump.rb
Created November 18, 2025 20:45
Get ObjectSpace heap dump via signal
# frozen_string_literal: true
# Heap Dump Signal Trap
#
# Send CONT signal to the process to trigger a heap dump.
#
# Usage:
# kill -CONT <pid>
#
# Output:
@skojin
skojin / source.txt
Created May 23, 2025 18:20
scroll to down bookmarklet
https://gist.github.com/nicole-ashley/242da6a653faa768d52d5be324f9ee60
# same as find_in_batches but with order support
# @usage InOrderedBatches.new(User.where(...), :updated_at, '2015-01-01'.to_time).each{|records| ... }
class InOrderedBatches
def initialize(scope, key, start, batch_size: 1000)
@scope = scope.order("#{key}, id").limit(batch_size)
@key = key
@value = start
@batch_size = batch_size
end
@skojin
skojin / navigation_helper.rb
Created November 15, 2011 14:40
Rails Helper to build 'active' links
module NavigationHelper
# get navigation 'active' css class if rule match
def nav_class(match_rules)
navigation_url_active?(nil, match_rules) ? 'active' : nil
end
# @param match_rules if :resource symbol, then match to url that starts withs specified url
# @param match_rules if :same symbol, then match exactly to url
# @param match_rules if hash {:controller, :action, :path, :method} and same yes with _not suffix (like :controller_not)
@skojin
skojin / controller.rb
Created September 16, 2022 13:45
batch set enabled flag from table with checkboxes
current_account.items.update_all(['enabled = (SELECT id IN (?))', Array(params[:ids])])
@skojin
skojin / irb_require_without_bundler_hack.rb
Created January 25, 2011 13:33
workaround to load irb specific gem (loaded in .irbrc) in bundler environment, like rails3 console
# Add all gems in the global gemset to the $LOAD_PATH so they can be used in rails3 console with bundler
if defined?(::Bundler)
$LOAD_PATH.concat Dir.glob("#{ENV['rvm_path']}/gems/#{ENV['rvm_ruby_string']}@global/gems/*/lib")
end
@skojin
skojin / if_jq.sh
Last active September 4, 2020 15:04
test input with jq, if ok then show input, otherwise exit with non 0 exit code
#!/bin/sh
# test input with jq, if ok then show input, otherwise exit with non 0 exit code
# echo '{"success": true}' | jq -e '.success | select(. == true)' # => {"success": true}
# echo '{"success": false}' | jq -e '.success | select(. == true)' # => exit code non 0
input=`cat`;
if echo "$input" | jq -e "$1" > /dev/null ; then
echo "$input"
@skojin
skojin / consider_my_requests_local.rb
Created January 24, 2011 15:17
same as config.consider_all_requests_local but for production and only for specified ip address, rails 3.2
silence_warnings do
ActionDispatch::Request::LOCALHOST = (ActionDispatch::Request::LOCALHOST + ['192.168.0.1']).freeze
end
ActionController::Base.module_eval do
def show_detailed_exceptions?
request.local?
end
end
@skojin
skojin / Dockerfile
Last active May 5, 2020 12:35
Dockerfile for http://luckyframework.org deployment
FROM crystallang/crystal:0.34.0 as builder
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential \
apt-transport-https curl ca-certificates gnupg2 apt-utils
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - \
&& apt-get install -y nodejs
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
@skojin
skojin / strong_params_generator.rb
Created February 12, 2018 16:01
generate strong params hash rule by real request hash
def to_permit_rule(h)
scalar = []
complex = {}
h.each do |k,v|
if v.is_a? Array
complex[k.to_sym] = []
elsif v.is_a? Hash
if v.keys.all?{|vk| vk =~ /\A\d+\z/ }
complex[k.to_sym] = to_permit_rule(v.values.first)
else