Skip to content

Instantly share code, notes, and snippets.

@jjb
jjb / config.ru
Created October 17, 2017 20:52
How to log file descriptor count in a rails or rack application
require_relative 'config/environment'
class FileDescriptorLogger
def initialize(app)
@app = app
end
def call(env)
if 1000==rand(1001)
unclosed_files = 0
@jjb
jjb / rails-timezones.md
Last active January 12, 2018 00:24
How to access the list of all timezones which are recognized by Rails

Time.find_zone! uses these two methods to look up strings:

  • ActiveSupport::TimeZone[time_zone]
  • TZInfo::Timezone.get(time_zone)

The lists they reference are these:

  • ActiveSupport::TimeZone.all.map(&:tzinfo).map(&:canonical_identifier) (150)
  • TZInfo::Timezone.all_identifiers (606)
@jjb
jjb / sleep.coffee
Created January 18, 2018 17:40
Sleep in coffeescript
sleep = (ms)->
date = new Date()
loop
break if (new Date())-date > ms
# from https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
# as foretold in a million passionate comments by programmers 'round the world,
# using this in business logic in javascript is a bad idea. but it sometimes comes in
# handy when trying to experiment with something in dev (like if you want to simulate
# a timeout)
@jjb
jjb / file.md
Created February 4, 2018 16:27
Active Record Connection Management in Rails 5.1 or lower

alert shell function

I keep mine in .zshrc. It could probably also be a standalone script named "alert" with chmod 755 (i think?) in ~/bin

# examples:
#   git push heroku master ; alert
#   bundle install ; alert
function alert {
  if (( $? == 0 )) then

5.0, no id specified

class SerialIdTest < ActiveRecord::Migration[5.0]
  def change
    create_table "test" do |t|
      t.integer "foo_id"
      t.string "foo_role"
    end
  end
@jjb
jjb / concat.rb
Last active March 8, 2019 19:10
Ruby String Interpolation vs. Concatenation performance benchmark
t = Time.now
a = []
10_000_000.times do
a << "a" + " " + "b"
end
puts Time.now.to_f - t.to_f
# https://github.com/my-company/my-project/branches/stale?page=7
buttons = document.getElementsByClassName("js-branch-delete-target text-red");
for(var i = 0; i < buttons.length; i++)
buttons[i].click();
class GraphqlController < ApplicationController
def create
ScoutApm::Transaction.rename("Graphql/" + params["_json"].first["operationName"])