Skip to content

Instantly share code, notes, and snippets.

@pgeraghty
pgeraghty / outlook_emails.rb
Last active May 2, 2024 12:08
Open Outlook email attachments with Ruby
# NB: Attachment#save seems to produce 0 byte files, probably would be resolved via rewind below
require 'mapi/msg'
msg = Mapi::Msg.open '/tmp/email.msg'
# view body
puts msg.properties.body
# save attachments
msg.attachments.each_with_index do |a, idx|
@pgeraghty
pgeraghty / shrink.sh
Created September 8, 2021 22:29
PDF shrink with Ghostscript
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook \
-dNOPAUSE -dBATCH -dColorImageResolution=150 \
-sOutputFile=output.pdf original.pdf
@pgeraghty
pgeraghty / settings.json
Created June 26, 2020 11:57
VS Code Settings - per-project shell history
{
"terminal.integrated.env.linux": {
"PROMPT_COMMAND": "history -a",
"HISTFILE":"${workspaceFolder}/.sh_history"
}
}
@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"]
@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 / 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 / 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 / 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 / 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 / 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