Skip to content

Instantly share code, notes, and snippets.

@michaelherold
michaelherold / monads.rb
Created November 7, 2018 17:42
It really seems like it should be easier to reduce a list of Results
require 'dry/monads/all'
include Dry::Monads::Result::Mixin
success = Success(:finished)
def merge(result, failures)
result
.or { |failure| Failure(failure + failures) }
.bind { Failure(failures) }
@michaelherold
michaelherold / README.md
Created August 15, 2018 01:28
The Newton-Raphson Method in Ruby

Newton-Raphson Method in Ruby

As part of an exercise in figuring out how to derive the continuous equation that is sampled by a discrete function, I went ahead and opened my Numerical Methods textbook and started going through the Newton-Raphson Method. This, plus my first ever tweet storm are the output.

A benchmark comparison between DateTime and Time in Ruby.

Instructions

This uses the excellent benchmark-ips library by Evan Phoenix. You'll have to install that with:

gem install benchmark-ips

To do the Rails comparisons, you'll need to be within a Rails environment. The easiest thing would be to save the benchmark script into a Rails project, make sure benchmark-ips is in your Gemfile, and then enter a rails c environment. In the console, enter:

@michaelherold
michaelherold / benchmark.rb
Last active August 18, 2017 16:58
Conditional string building in Ruby
#!/usr/bin/env ruby
# gem install benchmark-ips benchmark-memory
# ruby benchmark.rb
require "benchmark/ips"
require "benchmark/memory"
Benchmark.ips do |x|
x.report("join") do
@michaelherold
michaelherold / keybase.md
Created February 8, 2017 20:15
keybase.md

Keybase proof

I hereby claim:

  • I am michaelherold on github.
  • I am michaeljherold (https://keybase.io/michaeljherold) on keybase.
  • I have a public key ASC2imTPK_PBmFPtZknSKzIW-Dm8pqH3Bws2DiYaWE99Uwo

To claim this, I am signing this object:

#!/usr/bin/env ruby
hits = []
checks = {
#'_spec\.rb$' => ['focus:[:space:]*true'],
'\.rb$' => ['binding\.pry', 'debugger']
}
# Find the names of all the filenames that have been (A)dded (C)opied or (M)odified
filenames = `git diff --cached --name-only --diff-filter=ACM`.split("\n")
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
@michaelherold
michaelherold / gist:dc145c3eaac4349c0ad7
Created February 23, 2015 17:35
gulp-cache 0.2.7 error
Unhandled rejection TypeError: Cannot read property 'toString' of null
at defaultKey (/home/herold/code/project/node_modules/gulp-cache/index.js:17:39)
at objectAssign._getFileKey (/home/herold/code/project/node_modules/gulp-cache/lib/TaskProxy.js:56:32)
at objectAssign._checkForCachedValue (/home/herold/code/project/node_modules/gulp-cache/lib/TaskProxy.js:68:21)
at objectAssign.processFile (/home/herold/code/project/node_modules/gulp-cache/lib/TaskProxy.js:24:21)
at DestroyableTransform._transform (/home/herold/code/project/node_modules/gulp-cache/index.js:83:19)
at DestroyableTransform.Transform._read (/home/herold/code/project/node_modules/gulp-cache/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
at DestroyableTransform.Transform._write (/home/herold/code/project/node_modules/gulp-cache/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)
at doWrite (/home/herold/code/project/node_modules/gulp-cache/node_mo
@michaelherold
michaelherold / twitter-uri-issue.rb
Last active December 24, 2015 10:09
Bug report for issue on sferik/twitter
ruby-2.0.0 (main)> client = Twitter::REST::Client.new do |config|
config.consumer_key = <snip>,
config.consumer_secret = <snip>
end
=> #<Twitter::REST::Client:0x0000000749a370>
ruby-2.0.0 (main)> tweet = client.status(381940500032258049)
=> #<Twitter::Tweet:0x00000007752770>
ruby-2.0.0 (main)> uri = Twitter::Entity::URI.new(tweet.attrs[:entities][:urls][0])
=> #<Twitter::Entity::URI:0x00000007061808>
ruby-2.0.0 (main)> uri.display_url
@michaelherold
michaelherold / fix-gpg-errors.sh
Created May 22, 2012 02:01
Fixed Apt GPG Key Errors
#!/bin/sh
# This amazing little snippet saved me so much time
# Sourced from http://askubuntu.com/questions/85641
sudo apt-get clean
cd /var/lib/apt
sudo mv lists lists.old
sudo mkdir -p lists/partial
sudo apt-get clean
sudo apt-get update