Skip to content

Instantly share code, notes, and snippets.

View nateberkopec's full-sized avatar

Nate Berkopec nateberkopec

View GitHub Profile
@nateberkopec
nateberkopec / gcstat.md
Created March 8, 2017 18:25
A Guide to GC.stat

Most Ruby programmers don't have any idea how garbage collection works in their runtime - what triggers it, how often it runs, and what is garbage collected and what isn't. That's not entirely a bad thing - garbage collection in dynamic languages like Ruby is usually pretty complex, and Ruby programmers are better off just focusing on writing code that matters for their users.

But, occasionally, you get bitten by GC - either it's running too often or not enough, or your process is using tons of memory but you don't know why. Or maybe you're just curious about how GC works!

One way we can learn a bit about garbage collection in CRuby (that is, the standard Ruby runtime, written in C) is to look at the built-in GC module. If you haven't read the docs of this module, check it out. There's a lot of interesting methods in there. But for right now, we're just going to look at one: GC.stat.

GC.stat outputs a hash with a bunch of different numbers, but none of th

In thread: #<Process::Status: pid 56845 exit 1>
Global value: #<Process::Status: pid 56844 exit 0>
@nateberkopec
nateberkopec / benchmark.rb
Last active November 17, 2016 02:24
Why is Regex.match faster with this string in Ruby 2.4?
require 'benchmark/ips'
require 'securerandom'
BIG_STRING = <<-EOS
{:event_id=>"c18ae35e83234021bb78d8ba8a8ef2da", :timestamp=>"2016-11-16T23:27:52", :time_spent=>nil, :level=>40, :platform=>"ruby", :sdk=>{"name"=>"sentry-raven", "version"=>"2.1.3"}, :contexts=>{:server_os=>{"name"=>"Darwin", "version"=>"Darwin Kernel Version 16.1.0: Thu Oct 13 21:26:57 PDT 2016; root:xnu-3789.21.3~60/RELEASE_X86_64", "build"=>"16.1.0", "kernel_version"=>"Darwin nodachi.local 16.1.0 Darwin Kernel Version 16.1.0: Thu Oct 13 21:26:57 PDT 2016; root:xnu-3789.21.3~60/RELEASE_X86_64 x86_64"}, :runtime=>{"name"=>"ruby", "version"=>"ruby 2.3.2p217 (2016-11-15 revision 56796) [x86_64-darwin16]"}}, :logger=>"", :culprit=>"benchmarks/benchmark.rb in / at line 10", :server_name=>"nodachi.local", :release=>"425c535", :environment=>"default", :modules=>{"rake"=>"11.3.0", "concurrent-ruby"=>"1.0.2", "i18n"=>"0.7.0", "minitest"=>"5.9.1", "thread_safe"=>"0.3.5", "tzinfo"=>"1.2.2", "activesupport"=>"5.0.0.1", "builder"=>"3.2.2", "erubis"=>"2
@nateberkopec
nateberkopec / carryonpackinglist.txt
Created October 9, 2016 23:27
Carry-on packing list
This is the "everything" list. You'll be able to filter it via criteria (backpack only, cold weather, dress as male/female).
Please do suggest any additional items or recommend items for certain categories (your favorite carryon, for example).
BAGS
carryon bag
http://thewirecutter.com/reviews/travel-guide/#rolling
https://www.awaytravel.com/
backpack
http://thewirecutter.com/reviews/our-favorite-laptop-backpacks/
http://www.tropicalmba.com/best-digital-nomad-backpack/
@nateberkopec
nateberkopec / gpg
Created May 3, 2016 01:29
My public key. Send me some sick confidential dox now.
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
mQINBFcn/VEBEADCDbLxBfAR3N9kZe9nCQeJ8C4lVQPxKJKJJohydy+vvYLq3QPw
qmVffv/E2vF6xZDWitHU12ooiD4V1EqIjXPtCe92pJOi6DmERILo4/RAK6/cF2Tu
JSqmQEpL/vQhH2cIt5KGi1ApEhAMmPzatuG3PJusvcXZ1XNHINj0PW+SarDuWdGH
1fL1HMm5R6ERhQtT3NVO4ppgKVlxCI8S7z++UjoLU7NESqWhW7kwzFYWqcjqaL8N
XdLqMDjnvA7Nb2kefdluzMeww01nWB++O1zw0acSIl8wttkS0s1qLQWhunNIiT1V
odwRt85FFnYGM01szlQ98yWVqKjCilHR0ku7XNEy2A8tCQOc5bDuxCQphjmMouSb
Cjn0o7CWW9S9yJJd5VIbevABau761eukw5sM/Szc/QSBOv7+SWuyOpMgEEiQCQTF
class SomeController < ApplicationController
before_action :add_http_preload
private
def add_http_preload
# Always add a Link header to responses to this controller
http_preload script: "application.js", style: "application.css"
http_preload "myfont.woff", as: :font, push: false
# TODO: How should crossorigin work?
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
require 'json'
require 'open-uri'
require 'active_model'
require 'active_support'
require 'active_support/core_ext'
class Market
include ActiveModel::Model
ATTRIBUTES = [:id, :schedule, :address, :google_link]
begin
require "awesome_print"
AwesomePrint.irb!
rescue LoadError => e
puts e
end
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'