Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
struct Hints {
@halferty
halferty / mutate.py
Created August 31, 2016 19:26
Slowly mutate User-Agent over time for fun and profit.
#!/usr/bin/env python
import random
def mutator():
mutated = False
chars = list(mutator.mutatee)
for i, ch in enumerate(chars):
if not mutated and ch.isalnum() and random.random() > 0.999:
if ch.isalpha():
@halferty
halferty / localtime.sh
Created August 25, 2016 14:42
Use RVM in a shell script
#!/bin/bash
source ~/.rvm/scripts/rvm
rvm use 2.3.1@bin && ruby -e "require 'active_support/time'; puts DateTime.parse(\"$1\").in_time_zone('America/Los_Angeles')"
@halferty
halferty / reset_branch.sh
Last active January 17, 2018 19:09
Reset git directory to a known state - no questions asked (have a simpler version? are you sure? what if you're in the middle of a rebase and have untracked files?)
alias nuke-nuke-branch="nnuke_branch"
alias nuke-branch="nuke_branch"
function nuke_branch()
{
if [ $# -eq 1 ]; then
git clean -f -d
git rebase --abort
git branch -D tempbranch &> /dev/null
find . -name "post-checkout" | xargs rm -rf
[1] pry(main)> a = String.new
=> ""
[2] pry(main)> b = String.new
=> ""
[3] pry(main)> a.class.send(:define_method, "available_on_all_strings") { |a, b| "And the result is: " + a.to_s + b.to_s }
=> :available_on_all_strings
[4] pry(main)> a.available_on_all_strings(1, 2)
@halferty
halferty / ruby-metaprogramming-singleton_class.rb2
Last active June 23, 2016 23:41
Ruby metaprogramming with singleton_class
[1] pry(main)> a = Object.new
=> #<Object:0x007f90fc8f40f0>
[2] pry(main)> a.singleton_class
=> #<Class:#<Object:0x007f90fc8f40f0>>
[3] pry(main)> a.singleton_class.send(:define_method, "asdf") { |a| "hi there " + a }
=> :asdf
[4] pry(main)> a.asdf
ArgumentError: wrong number of arguments (0 for 1)
from (pry):3:in `block in __pry__'
[5] pry(main)> a.asdf("gg")
@halferty
halferty / ruby-metaprogramming-to_proc.rb
Last active June 23, 2016 22:18
Ruby metaprogramming w/ to_proc
[1] pry(main)> require 'json'
=> true
[2] pry(main)> [1, 2, 3].to_json
=> "[1,2,3]"
[3] pry(main)> [[1, 2, 3], [4, 5, 6], [7, 8, 9]].map &:to_json
=> ["[1,2,3]", "[4,5,6]", "[7,8,9]"]
[4] pry(main)> JSON.parse("[1,2,3]")
=> [1, 2, 3]
[5] pry(main)> ["[1,2,3]", "[4,5,6]", "[7,8,9]"].map &JSON.method(:parse)
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
@halferty
halferty / convert_pdf.bat
Last active June 13, 2016 08:59
Convert PDF to PNGs, one per page
"C:\Program Files\gs\gs9.19\bin\gswin64.exe" -dNOPAUSE -dBATCH -sDEVICE=png16m -r600 -dDownScaleFactor=3 -sOutputFile=C:\Users\ed\Desktop\test_images\test-%%03d.jpg C:\Users\ed\Desktop\test.pdf
pause
@halferty
halferty / glfptr.cpp
Created March 31, 2016 04:30
gl grab function pointer
void (*glGenBuffers)(GLsizei n, GLuint * buffers) = (void (*)(GLsizei, GLuint *))wglGetProcAddress("glGenBuffers");
@halferty
halferty / findclosest.scala
Created March 18, 2016 19:19
Find closest item in list to target using scala Ordering
case class Asset(width:Int, url:String)
val a = Asset(100, "asdf") :: Asset(200, "asdf") :: Asset(210, "asdf") :: Nil
val target = 209
a.min(Ordering.by((asset:Asset) => Math.abs(target - asset.width)))