Skip to content

Instantly share code, notes, and snippets.

@nilbus
nilbus / rescue_proof.rb
Created February 4, 2019 18:47
A class with a new constant name can be rescued by either name
[3] pry(main)> class E1 < RuntimeError
[3] pry(main)* end
=> nil
[4] pry(main)> E2 = E1
=> E1
[5] pry(main)> E2
=> E1
[6] pry(main)> def r1
[6] pry(main)* raise E2
[6] pry(main)* rescue E1
@nilbus
nilbus / output.txt
Created November 3, 2017 14:18
Vagrant embedded ruby fails on OSX after fresh install on new hardware w/ TimeMachine backup restored
$ /opt/vagrant/embedded/bin/ruby
/opt/vagrant/embedded/lib/ruby/2.4.0/rubygems/specification.rb:17:in `require': dlopen(/opt/vagrant/embedded/lib/ruby/2.4.0/x86_64-darwin13/stringio.bundle, 9): no suitable image found. Did find: (LoadError)
/opt/vagrant/embedded/lib/ruby/2.4.0/x86_64-darwin13/stringio.bundle: malformed mach-o image: load command #17 length (0) too small in /opt/vagrant/embedded/lib/ruby/2.4.0/x86_64-darwin13/stringio.bundle
/opt/vagrant/embedded/lib/ruby/2.4.0/x86_64-darwin13/stringio.bundle: malformed mach-o image: load command #17 length (0) too small in /opt/vagrant/embedded/lib/ruby/2.4.0/x86_64-darwin13/stringio.bundle - /opt/vagrant/embedded/lib/ruby/2.4.0/x86_64-darwin13/stringio.bundle
from /opt/vagrant/embedded/lib/ruby/2.4.0/rubygems/specification.rb:17:in `<top (required)>'
from /opt/vagrant/embedded/lib/ruby/2.4.0/rubygems.rb:1334:in `require'
from /opt/vagrant/embedded/lib/ruby/2.4.0/rubygems.rb:1334:in `<module:Gem>'
from /opt/vagrant/embedded/lib/ruby/2.4.0/rubygems.rb:
User.new.job # => unsaved Job
User.create!.job # => saved Job
User.last.job # => existing Job
@nilbus
nilbus / npm_install_error.txt
Created July 20, 2017 15:24 — forked from anonymous/npm_install_error.txt
npm install fails to symlink in Docker for Windows with app in mounted filesystem
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! node v4.8.4
npm ERR! npm v2.15.11
npm ERR! path ../coffee-script/bin/coffee
npm ERR! code EINVAL
npm ERR! errno -22
npm ERR! syscall symlink
npm ERR! EINVAL: invalid argument, symlink '../coffee-script/bin/coffee' -> '/exercism/node_modules/lineman/node_modules/.bin/coffee'
@nilbus
nilbus / garbage_collect_block_vs_string.rb
Last active July 14, 2017 16:48
Surprise! In JRuby, Hash#fetch with a block to specify a default string is slower than passing the string as a 2nd argument.
require 'benchmark'
def fetch_with_block(arg)
arg.fetch(:sym) { "str" }
end
def fetch_with_str(arg)
arg.fetch(:sym, "str")
end
@nilbus
nilbus / results.md
Last active May 7, 2017 14:18
Benchmark to prove the validity of Avdi's approach to methods as constants

Summary

Feel free to use Avdi-style constants as methods when not calling the method repeatedly in a short period of time. Otherwise, consider the performance trade-off.

  • GC for this example took about 1.8ms per 1000 calls.
  • For significant numbers of iterations, using constants sped this example by 10x in reduced GC overhead
  • Each call in this test required GC of 10 short strings and 1 Hash.
@nilbus
nilbus / data.rake
Created April 10, 2017 02:44
Generate comments using existing data for performance testing
namespace :generate do
desc "generate N comments, typically for performance testing"
task comments: [:connection] do
n = ENV['N'].to_i
abort 'specify the number of comments to generate: N=<count>' if n.zero?
puts "Generating #{n} comments"
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO comments (submission_id, user_id, body, html_body, created_at, updated_at) (
SELECT
submission_id,
@nilbus
nilbus / deploy.sh
Created February 11, 2017 21:12
nilbus.com docker service update script
#!/bin/sh
# Used with https://github.com/nilbus/docker-hook to deploy $IMAGE
# when triggered by a Docker Hub webhook.
IMAGE='nilbus/nilbus.com'
docker pull $IMAGE
docker service update http --image $IMAGE
@nilbus
nilbus / brew-doctor.txt
Last active November 13, 2016 02:18
Relevant info related to homebrew-fuse issue
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!
Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
Unexpected dylibs:
/usr/local/lib/libcspec.0.dylib
def update
find_user
if @user.update_attributes(user_params)
respond_to do |format|
format.html {
redirect_to admin_user_path(@user)
flash[:notice] = 'User updated'
}
format.js {