Skip to content

Instantly share code, notes, and snippets.

puts "event".ljust(20) +
"line".ljust(10) +
"id".ljust(20) +
"binding".ljust(30) +
"classname".ljust(10)
set_trace_func proc { |event, file, line, id, binding, classname|
puts event.to_s.ljust(20) +
line.to_s.ljust(10) +
id.to_s.ljust(20) +
a = [1,2]
b = case a
when [1,1]
"hi"
when [2,2]
"hello"
when [1,2]
"heya"
end
@halferty
halferty / nuke-branch.sh
Last active August 29, 2015 14:06
nuke-branch: For when you just want to make your local branch match a remote
# ~/.bashrc
alias nuke-branch="nuke_branch"
function nuke_branch()
{
if [ $# -eq 1 ]; then
git fetch upstream
git branch -D tempbranch &> /dev/null
git checkout -b tempbranch
git branch -D $1
object HelloWorld {
class StringHelper(s: String) {
def exclaim() = { s + "!!!" }
}
def main(args: Array[String]) {
implicit def stringWrapper(s: String) = new StringHelper(s)
println("asdf".exclaim())
}
}
@halferty
halferty / post_spec.rb
Created November 13, 2014 00:27
WebMock wrapper to promote code readability while restricting stubbed responses to specific tests.
# spec/models/post_spec.rb
require "spec_helper"
require "post"
describe Post do
it "gets a post by id" do
mock_apis([post_mocker]) do
post = Post.get_by id: "dummy-id"
@halferty
halferty / aaa.rb
Last active August 29, 2015 14:10
more monads
class Object; def if(&block); (yield(self))? self : nil; end; end;
class String; def unless_empty; self.if { |a| a.size > 0 }; end; end;
@halferty
halferty / autoload_handlebars_templates.js
Last active August 29, 2015 14:13
Autoload handlebars templates from <script> tags.
// Autoload handlebars templates from <script> tags.
$("script[type='text/x-handlebars-template'").each(function() {
var $this = $(this);
window[$this[0].id] = Handlebars.compile($this.html());
});
$("script[type='text/x-handlebars-partial'").each(function() {
var $this = $(this);
Handlebars.registerPartial($this[0].id, $this.html());
});
<?php
function callback($buffer) {
return (str_replace("thar", "there", $buffer));
}
ob_start("callback");
?>
<h1>Hi thar!</h1>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int isPrime(int64_t);
int main(int argc, char *argv[]) {
int64_t testPrime = 0xffffffffffffffc5LL;
printf("%d", isPrime(testPrime));
return 0;
@halferty
halferty / point_from_3_planes.c
Created March 23, 2015 08:38
Linear algebra basic 101 stuff. I gotta start somewhere, I suppose. Sacrificing efficiency for readability here.
void PointFrom3Planes(float x1, float y1, float z1, float d1, float x2, float y2, float z2, float d2, float x3, float y3, float z3, float d3, float *px, float *py, float *pz) {
// Calculate matrix of minors
float matrixOfMinors[3][3] = {
{
(y2 * z3 - y3 * z2),
(x2 * z3 - x3 * z2),
(x2 * y3 - x3 * y2),
},
{
(y1 * z3 - y3 * z1),