Skip to content

Instantly share code, notes, and snippets.

@pgeraghty
pgeraghty / gist:bd3cd6ac42c5cf7d9e06
Created February 2, 2015 01:57
Test HTML embed
<a href="#">Test</a>
@pgeraghty
pgeraghty / format_compact_duration.rb
Created February 24, 2015 01:42
format compact duration as days, hours, minutes, seconds e.g. 3d23h10s
# seconds = duration in seconds as integer i.e. time.to_i
[
seconds / (60*60*24), # days
seconds % (60*60*24) / (60*60), # hours
seconds % (60*60) / 60, # minutes
seconds % 60 # seconds
].zip(%w(d h m s)).map { |u| u.join unless u.first.zero? }.compact.tap { |a| a << '<1s' if a.none? }.join
# Uses Object#tap rather than Rails Object enhancements like Object#presence
@pgeraghty
pgeraghty / gfw.js
Last active August 29, 2015 14:16
W2V example script - Global Forest Watch
// W2V script intended for http://www.globalforestwatch.org/map/3/15.00/27.00/ALL/grayscale/loss,forestgain?begin=2001-01-01&end=2013-01-01&threshold=30
w2v.info("Injecting polyfill");
// Inject W2V polyfill so that this script will run in normal browsers without errors
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)){ return; }
js = d.createElement(s); js.id = id;
js.onload = function(){
@pgeraghty
pgeraghty / application.rb
Created March 15, 2015 10:13
Memory profiling for requires - Ruby
if ENV['MEMORY_PROFILING']
require 'benchmark'
def require(file)
puts `pmap #{Process.pid} | grep 'total'`
puts Benchmark.measure('') { super }.format("%t require #{file}")
end
end
@pgeraghty
pgeraghty / static_input.rb
Created September 4, 2015 19:48
Simple Form static input as used by Bootstrap 3+ to have correct alignment in forms
module SimpleForm
module Inputs
class StaticInput < SimpleForm::Inputs::StringInput
def input
template.content_tag(:p, object.send(attribute_name), class: 'form-control-static')
end
end
end
end
@pgeraghty
pgeraghty / float_alternatives.rb
Last active July 21, 2016 19:40
Benchmark Rational vs. BigDecimal
require 'benchmark/ips'
require 'bigdecimal'
Benchmark.ips do |x|
x.report('Rational from String') { '214.04'.to_r - '74.79'.to_r + '74.79'.to_r > '214.04'.to_r }
x.report('Rational from Float') { 214.04.to_r - 74.79.to_r + 74.79.to_r > 214.04.to_r }
x.report('BigDecimal from String') { BigDecimal.new('214.04') - BigDecimal.new('74.79') + BigDecimal.new('74.79') > BigDecimal.new('214.04') }
@pgeraghty
pgeraghty / redis-monitor.log
Created July 26, 2019 23:29
Redis standalone MONITOR log
# RUBY
1564181439.071863 [0 99.99.99.99:43608] "multi"
1564181439.131934 [0 99.99.99.99:43608] "sadd" "queues" "from_sk_ruby"
1564181439.131955 [0 99.99.99.99:43608] "lpush" "queue:from_sk_ruby" "{\"queue\":\"from_sk_ruby\",\"class\":\"Sample::MyWorker\",\"args\":[{\"test\":\"payload\",\"activity_id\":15838549}],\"retry\":true,\"jid\":\"ab8ed2a13e41756c5a4dde89\",\"created_at\":1564181430.3014073,\"enqueued_at\":1564181439.043673}"
1564181439.132031 [0 99.99.99.99:43608] "exec"
1564181439.180776 [0 99.99.99.99:43608] "multi"
1564181439.223040 [0 99.99.99.99:43608] "sadd" "queues" "from_sk_ruby"
1564181439.223064 [0 99.99.99.99:43608] "lpush" "queue:from_sk_ruby" "{\"queue\":\"from_sk_ruby\",\"class\":\"Sample::MyWorker\",\"args\":[{\"test\":\"payload\",\"activity_id\":15838549}],\"retry\":true,\"jid\":\"ab8ed2a13e41756c5a4dde89\",\"created_at\":1564181430.3014073,\"enqueued_at\":1564181439.1471574}"
@pgeraghty
pgeraghty / sidekiq-client-benchmark-output.txt
Created July 27, 2019 00:33
Sidekiq::Client.push benchmark output
# ORIGINAL
Time to queue remotely: 00:00:00.236023892
Time to queue remotely: 00:00:00.123225651
Time to queue remotely: 00:00:00.136234454
Time to queue remotely: 00:00:00.140092191
Time to queue remotely: 00:00:00.120226790
Time to queue remotely: 00:00:00.120232020
Time to queue remotely: 00:00:00.123584921
Time to queue remotely: 00:00:00.148454025
@pgeraghty
pgeraghty / alpine_crystal_static_compilation.cr
Last active August 30, 2019 21:36
Allow compilation of statically-linked binary for Crystal Lang on Alpine
# Hack to avoid segfault during compilation of statically-linked binary on Alpine
{% if flag?(:static) %}
require "llvm/lib_llvm"
require "llvm/enums"
{% end %}
@pgeraghty
pgeraghty / Dockerfile
Created March 16, 2020 17:53
Basic Dockerfile for multi-stage build of statically-linked binary via Crystal shards
FROM crystallang/crystal:0.33.0-alpine-build as builder
WORKDIR /app
COPY . /app
RUN shards build --static --release --no-debug
FROM scratch
WORKDIR /app
COPY --from=builder /app/bin /bin
ENTRYPOINT ["/bin/<YOUR_BINARY_NAME"]