Skip to content

Instantly share code, notes, and snippets.

View avdi's full-sized avatar

Avdi Grimm avdi

View GitHub Profile
module Enumerable
def each_with_emptiness
return to_enum(__callee__) unless block_given?
if empty?
yield nil, true
else
each do |element|
yield element, false
end
def get_account_balance(fail: false)
if fail
[:error, "I literally can't even"]
else
"$1234.56"
end
end
@avdi
avdi / unindent.rb
Created September 16, 2014 19:00
Stripping indentation from heredocs
TEXT = <<EOF
See, the interesting thing about this text
is that while it seems like the first line defines an indent
it's actually the last line which has the smallest indent
there are also some blank lines
both with and without extra spaces in them
and it just goes on and on
require 'equalizer'
class EqualizableStruct < Struct
def initialize *args, &block
class << self
include Equalizer.new(*members)
end
super
end
@avdi
avdi / gist:9038972
Created February 16, 2014 19:00
Get syntax highlighted source code for pasting into e.g. Google Docs on Linux
# You will need the pygments and xclip packages
# This example highlights some Bash source code
# '-O noclasses=true' tells pygments to embed colors inline in the source
# the '-t text/html' option tells xclip what "target" to specify for the selection
pygmentize -l bash -f html -O noclasses=true mysource.sh | xclip -selection clipboard -t text/html
@avdi
avdi / gist:8995165
Created February 14, 2014 03:11
One line web server that Chrome doesn't like
while true; do { echo -e 'HTTP/1.1 200 OK\r\n\r\n'; echo "Hello, world"; } | nc -l 1234; done
c.filter_sensitive_data('<AUTH FILTERED>') {|interaction|
Array(interaction.request.headers['Authorization']).first
}
@avdi
avdi / rubytapas-theme.el
Created January 12, 2014 05:03
RubyTapas color theme for emacs (modified Molokai)
;;; rubytapas-theme.el --- rubytapas theme with Emacs theme engine
;; Copyright (C) 2013 by Adam Lloyd
;; Copyright (C) 2013 by Syohei YOSHIDA
;; Copyright (C) 2013 by Avdi Grimm
;; Author: Avdi Grimm <[email protected]>
;; URL:
;; Version:
;; X-Original-Version: 0.01
@avdi
avdi / gist:7990684
Created December 16, 2013 17:20
Streaming a URL to a file in Elixir
def download_video(Episode[filename: filename] = episode) do
Stream.resource(fn -> begin_download(episode) end,
&continue_download/1,
&finish_download/1)
|> File.stream_to!(filename)
|> Stream.run
end
def begin_download(Episode[video_url: video_url,
account: Account[login: login,
require "thread"
Thread.abort_on_exception = true
def none
counter = 0
threads = 10.times.map do
Thread.new do
100.times do