Skip to content

Instantly share code, notes, and snippets.

log:/[1-9][0-9]{4,}ms/
log: END: current_user\[.*\] \([1-9][0-9]{1,}.\d+ seconds\)
@austra
austra / gist:e7324a8057da26cf0384b5808f8654ca
Last active July 21, 2019 21:51
delete jobs from resque by class
With Resque:
queue_name = 'default'
jobs = Resque.data_store.peek_in_queue(queue_name, 0, 500_000); nil;
deleted_count = 0
jobs.each do |job|
decoded_job = Resque.decode(job)
if decoded_job['class'] == "My::Class::Name"
Resque.data_store.remove_from_queue(queue_name, job)
@austra
austra / gist:b633d94f0c01636a04f23f49c1eaa353
Created January 31, 2019 21:04
Basic Encrypt/Decryption
def aes256_cbc_encrypt(key, data, iv)
if !(key.bytesize === 32)
key = Digest::SHA256.digest(key)
end
if !(iv.bytesize === 16)
iv = Digest::MD5.digest(iv)
end
#padding = 16 - (data.length % 16)
We tried this about a year or so ago. We didn't see any memory or performance savings at the time, but I'm not sure about our methodology...
Eveything I've read about this indicates decent to great memory/peformance gains:
(Nate Berkopec is big in the Rails perfomance game, and recommends everyone use it.)
https://www.speedshop.co/2017/12/04/malloc-doubles-ruby-memory.html
Other good writeups:
http://engineering.appfolio.com/appfolio-engineering/2018/2/1/benchmarking-rubys-heap-malloc-tcmalloc-jemalloc
https://www.mikeperham.com/2018/04/25/taming-rails-memory-bloat/
@austra
austra / Odd Occurrences In Array
Last active January 5, 2018 19:15
Odd Occurrences In Array
Now that I found this... Lets do the next one.
https://codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/
a = [9,3,9,3,9,7,9]
a.group_by(&:itself).inject(0){|hash, (k,v)| v.first if v.size.odd?}
# Could also use transform_values instead of inject on Ruby 2.4
Turns out this one is easy with XOR exlusive or operator ^.
@austra
austra / Largest Binary Gap
Last active December 14, 2017 21:23
Largest Binary Gap
I read about this problem and thought I would try it. Later I found it is a question here too:
https://codility.com/programmers/lessons/1-iterations/binary_gap/
My first instinct was, "I know regex can do this, but I hate regex" (Which is also a good reason to get better at regex).
I couldn't quite hoop the syntax so I moved on. Here is what I came up with:
def max_binary_gap(n)
max_gap = 0
arr = n.to_s(2).chars
while arr.size > 2 do
@austra
austra / puma_ssl.sh
Created May 22, 2017 20:42 — forked from PragmaticEd/puma_ssl.sh
Run Puma server with https
# 1) Create your private key (any password will do, we remove it below)
$ cd ~/.ssh
$ openssl genrsa -des3 -out server.orig.key 2048
# 2) Remove the password
$ openssl rsa -in server.orig.key -out server.key
@austra
austra / gist:43de145033ab881cf0012ed942504848
Created March 16, 2017 16:41
Ruby Benchmark with ObjectSpace
require 'objspace'
require 'benchmark'
Benchmark.bmbm do |bm|
bm.report("using [] :") do
GC.start
mem_before = ObjectSpace.memsize_of_all
before = GC.stat[:total_allocated_objects]
@austra
austra / stock
Last active January 11, 2017 21:49
stock
dis,5,108.59
nflx,3,130.91
@austra
austra / gist:a007b49774339b70c9bd8aedd6f5ecce
Last active December 16, 2016 20:05
Rails REST Resources
http://www.sm-cloud.com/ruby-json-mapping/
http://jsonapi-rb.org/#rails
https://github.com/trailblazer/representable
https://github.com/trailblazer/roar
https://github.com/remiprev/her
https://github.com/lostisland/faraday_middleware/wiki
http://blog.excelwithcode.com/ruby-api-clients.html