Skip to content

Instantly share code, notes, and snippets.

Avatar
🦄

Eric Hodel drbrain

🦄
View GitHub Profile
@drbrain
drbrain / console
Last active Oct 19, 2020
Ruby frozen string interning
View console
$ ruby not_frozen.rb
400600
# The string "a" is embedded inside the String object which is 40 bytes
#
# So 400600/40 = ~10,015 string objects
#
# (IIRC, up to 24 bytes of string may be embedded in the String)
$ ruby frozen.rb
View result
$ ruby -v t.rb
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]
Warming up --------------------------------------
if else 186.397k i/100ms
case with comparison 149.445k i/100ms
case all range 127.260k i/100ms
case hash cheat 190.074k i/100ms
Calculating -------------------------------------
if else 3.447M (± 7.5%) i/s - 17.149M in 5.007151s
case with comparison 2.388M (±18.2%) i/s - 11.657M in 5.065894s
View extract_certificates.rb
#!/usr/bin/env ruby
##
# Use this tool to extract X509 certificates from an HTTP server you want to
# connect to that is using a self-signed certificate. This is a
# Trust-on-First-Use operation so you are still at risk of various attacks
# including man-in-the-middle.
#
# This does allow you to uniformly use VERIFY_PEER across all HTTPS
# connections, as you now will be verifying the possibly-suspect connection
@drbrain
drbrain / Many runs
Last active May 17, 2019
benchmark/ips can mislead you
View Many runs
Warming up --------------------------------------
range 60.821k i/100ms
args 63.056k i/100ms
Calculating -------------------------------------
range 774.236k (± 7.5%) i/s - 3.893M in 5.057747s
args 797.590k (± 6.0%) i/s - 3.973M in 5.000220s
Warming up --------------------------------------
range 64.468k i/100ms
args 65.528k i/100ms
Calculating -------------------------------------
View nesting.rb
module A
module B
puts "module B inside module A nesting: #{Module.nesting}"
module_function
def show_nesting
puts "show_nesting nesting: #{Module.nesting}"
end
@drbrain
drbrain / Z̤̯̤͈̲̠͋͟A̷̞͌̍̆̓L̺̙͖G̜̣̱̃̍́ͮO̤͙̫͔̔́̏͐̇͡.rb
Created Dec 1, 2017
color Z̤̯̤͈̲̠͋͟A̷̞͌̍̆̓L̺̙͖G̜̣̱̃̍́ͮO̤͙̫͔̔́̏͐̇͡
View Z̤̯̤͈̲̠͋͟A̷̞͌̍̆̓L̺̙͖G̜̣̱̃̍́ͮO̤͙̫͔̔́̏͐̇͡.rb
def color(i)
color = 16 + (i * 13) % 216
"\e[38;5;#{color}m"
end
5.times { puts }
zalgo = "Z̤̯̤͈̲̠͋͟A̷̞͌̍̆̓L̺̙͖G̜̣̱̃̍́ͮO̤͙̫͔̔́̏͐̇͡!̷͙͉͖̋ͩ̓̇̿"
@drbrain
drbrain / ugh.rb
Last active Nov 9, 2017
You can't use `enum_for __method__` with inheritance 😢
View ugh.rb
class A
def m(&block)
#return enum_for __method__ unless block_given?
# The above calls the B implementation of #m so instead we have to create
# our own Enumerator that's bound to this class' method so we can run the
# with-block version of this method
unless block_given? then
enum = Enumerator.new do |yielder|
method = A.instance_method(__method__).bind self
@drbrain
drbrain / run
Created Nov 2, 2017
Don't convert hash keys, it's slower than using the "natural" key of the input data
View run
$ ruby -v t.rb
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin17]
Lookup
Warming up --------------------------------------
Symbol Hash, Symbol Key
259.197k i/100ms
String Hash, String Key
231.953k i/100ms
Symbol Hash, String Key
203.959k i/100ms
View unfuck-vmware-net.sh
#!/usr/bin/env bash
set -e
[ -n "$DEBUG" ] && set -x
banner() { printf -- "-----> $*\n"; }
banner "Restarting VMware networking"
banner "Stopping networking"
sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --stop
View cross.rake
require 'rake/tasklib'
require 'rake/clean'
##
# Creates a cross-compiled library that mruby-cli can use to link with
# each cross-built command.
#
# CrossLibrary.new "curl" do |cross|
# cross.release_name = "curl-7.28.0"
# cross.url = "https://curl.example/download/#{cross.release}.tar.gz"
You can’t perform that action at this time.