Skip to content

Instantly share code, notes, and snippets.

View gist:c72303df32dc4dc7860c
ruby-install -p -p -p -p -p -p -p -p -p https://raw
bhb / exception_logging_example.rb
Created May 1, 2014
Examples for logging Ruby exceptions
View exception_logging_example.rb
raise "flunk!"
rescue => e
puts "Error was #{e.message}" # Error was flunk!
puts "Error was #{e}" # Error was flunk!
puts "Error was #{e.inspect}" # Error was #<RuntimeError: flunk!>
bhb / stream.rb
Created Jan 22, 2015
Streaming data through Ruby
View stream.rb
# based on
# Toying around with alternatives to nested method calls
# i.e. foo(bar(baz))
# and method chaining
# i.e.
def stream(data, *methods)
methods.reduce(data) do |d, m|
case m
bhb / stats.rb
Last active Aug 29, 2015
Compute stats for array of numbers
View stats.rb
def percentile(arr, pcent)
sorted = arr.sort
percent_ranks = (1..arr.length).map { |i| (100.0 / sorted.length) * ( i - 0.5) }
if pcent < percent_ranks.first
elsif pcent > percent_ranks.last
bhb / c
Created Mar 11, 2015
Copy text into clipboard
View c
#! /usr/bin/env ruby
# file: ~/bin/c
# usage: highlight lines and pipe them to stdout in your editor. nothing
# changes except they are copied to your paste buffer. should be modified to
# work with linux using xclip
STDOUT.sync = true
View gist:fcdabe97816137cfc611
brew install opam libffi libreadline-dev
git clone grenchman
cd grenchman
opam init (choose y to update .bash_profile or similar)
[open new shell]
opam install ocamlfind core async ctypes
ocamlbuild -use-ocamlfind -lflags -cclib,-lreadline grench.native
ln -s $PWD/grench.native ~/bin/grench
grench lein test
bhb / translate_midje.clj
Created Mar 30, 2015
Translate Midje specs to clojure.test tests
View translate_midje.clj
;; Converts a midje test file into a clojure.test test file
;; Adapted from
;; Usage:
;; 1. Add [bultitude "0.2.6"] to project.clj
;; 2. Load this file into repl
;; 3. (require 'circleci.translate-midje)
;; 3. (circleci.translate-midje/test-rewrite "test/yourproject/test/foo_test.clj")
;; It won't do everything perfectly (namely, it doesn't translate the 'provided' macro)
View secrets.repl
;; gorilla-repl.fileformat = 1
;; **
;;; # "SECRETS"* of Clojure development
;;; \* one or more tips may not be a secret to you!
;;; For months after getting started with Clojure, I understood how to write functions, but not how to **develop programs**. I did have a good grasp of the processes, idioms, and ways to structure larger code bases. As a result, I largely developed programs the same way I did with Ruby: using TDD with lots of implicit state strewn throughout my programs.
;;; I'm still learning, but I want to share a few of the key ideas I wish I had known when I first started out.
View gist:22909
require "openssl"
require "net/smtp"
Net::SMTP.class_eval do
def do_start(helodomain, user, secret, authtype)
raise IOError, 'SMTP session already started' if @started
check_auth_args user, secret, authtype if user or secret
sock = timeout(@open_timeout) {, @port) }
bhb / foo.rb
Created Nov 13, 2008
View foo.rb
class Foo
def bar
puts "Hello, world!"