Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
@havenwood
havenwood / cipher.rb
Created Nov 5, 2019
Ruby Example of AES-256 in CBC Mode for consti on #ruby IRC
View cipher.rb
secret = 'cicada'
key = 'wombat'
cipher = OpenSSL::Cipher::AES256.new :CBC
cipher.encrypt
salt = OpenSSL::Random.random_bytes 16
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1 key, salt, 20000, 32
iv = cipher.random_iv
View async_fetch.rb
require 'async'
require 'async/barrier'
require 'open-uri'
def async_fetch(*urls)
barrier = Async::Barrier.new
Async do
urls.map do |url|
barrier.async { URI.open url, &:read }
@havenwood
havenwood / .pryrc
Created Sep 17, 2020
My `~/.pryrc` file
View .pryrc
Pry.config.prompt = Pry::Prompt[:simple]
Pry.config.pager = false
##
# Support refinements at top level in Pry.
Pry.hooks.add_hook :when_started, :context_switch do |_output, _binding, pry|
pry.binding_stack = [TOPLEVEL_BINDING]
end
##
@havenwood
havenwood / chruby_ruby-build.sh
Created Nov 27, 2012
On Debian, Ubuntu, or Mint: install Ruby with chruby and ruby-build
View chruby_ruby-build.sh
# Install apt-get packages:
sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config
# Install chruby:
cd
wget https://github.com/downloads/postmodern/chruby/chruby-0.2.3.tar.gz
tar -xzvf chruby-0.2.3.tar.gz
cd chruby-0.2.3
sudo make install
@havenwood
havenwood / to_struct.rb
Created Sep 1, 2020
An anonymous Struct in pure Ruby (looking forward to real ones in Ruby 3)
View to_struct.rb
module ToStruct
refine Hash do
def to_struct(class_name = nil)
if class_name
Object.const_set(class_name, Struct.new(*keys))
Object.const_get(class_name).new(*values)
else
Struct.new(*keys).new(*values)
end
end
@havenwood
havenwood / chruby_local.sh
Last active Sep 1, 2020
Install the latest stable Ruby as a local user (non-root).
View chruby_local.sh
##
# A non-root installation of the latest Ruby with chruby
# Install chruby (https://github.com/postmodern/chruby#readme)
mkdir -p $HOME/src
cd $HOME/src
wget -O chruby-0.3.9.tar.gz https://github.com/postmodern/chruby/archive/v0.3.9.tar.gz
tar -xzvf chruby-0.3.9.tar.gz
cd chruby-0.3.9/
PREFIX=$HOME/.chruby make install
@havenwood
havenwood / benchmark_results.rb
Last active Aug 31, 2020
Benchmarking serialization speed of YAML, JSON, Marshal, and MessagePack in Ruby, JRuby, and Rubinius
View benchmark_results.rb
# ruby-2.5.0
user system total real
YAML 15.112795 0.030577 15.143372 ( 15.190573)
JSON 0.648957 0.001520 0.650477 ( 0.652856)
Marshal 0.474775 0.000922 0.475697 ( 0.477678)
MessagePack 0.326430 0.001763 0.328193 ( 0.330159)
# ruby-2.4.3
user system total real
YAML 20.400000 0.050000 20.450000 ( 20.517600)
@havenwood
havenwood / same_generation_and_age.rb
Created Aug 27, 2020
A guess at what old_relik was looking for in #ruby IRC
View same_generation_and_age.rb
def recurse_ages(hash, depth: 0, history: [])
hash.flat_map do |key, value|
if value.instance_of?(Hash)
history[depth] = key
method(__method__).call value, depth: depth.succ, history: history
else
[history[0..depth.pred], value]
end
end
end
@havenwood
havenwood / old_relik.rb
Created Aug 26, 2020
Clarifying a Question on #ruby IRC
View old_relik.rb
h1 = { a: true, b: { c: [1, 2, 3], d: [42] } }
h2 = { a: false, b: { x: [3, 4, 5], d: [42] } }
h1.deep_intersection(h2)
#=> ?
@havenwood
havenwood / fallthrough.rb
Last active Aug 5, 2020
Fallthrough case statement in Ruby
View fallthrough.rb
value = :initial
catch :redo do
case value
when :initial
puts 'initial'
value = :changed
redo
when :changed
puts 'changed'
You can’t perform that action at this time.