Skip to content

Instantly share code, notes, and snippets.

View nyx's full-sized avatar
👨‍🚀
space explorer

Andrew Andkjar nyx

👨‍🚀
space explorer
View GitHub Profile
@nyx
nyx / topology.clj
Created April 6, 2014 23:20
A simple Storm topology defined in Clojure that doubles an infinite streams of random values with some parallelism
(ns dawn.topology
(:import [backtype.storm StormSubmitter LocalCluster])
(:use [backtype.storm clojure config])
(:gen-class))
(defspout random-spout ["random-float"]
[conf context collector]
(spout
(ack [id]
(println "random-spout ack"))
@nyx
nyx / hash-dig.rb
Created February 4, 2014 15:38
easily dig values out of deeply nested hash with useful index error reporting
class Hash
def dig(*keys)
#puts "dig: #{keys.inspect} #{self.inspect}"
if keys.empty?
return self
else
begin
v = self.fetch(keys.first)
return v if(keys.size.eql?(1))
v.dig(*keys.drop(1))
@nyx
nyx / make-osx-ram-disk.rb
Last active April 28, 2016 11:34
user-friendly (and quick-and-dirty) Ruby script to generate RAM disks on Mac OSX
BYTES_PER_SECTOR=512 # AFAIK this is constant for Mac OSX RAM disks
BYTES_PER_KILOBYTE=1024
BYTES_PER_MEGABYTE=1024*1024
BYTES_PER_GIGABYTE=1024*1024*1024
def bytes_to_sectors(bytes)
sectors = (bytes / BYTES_PER_SECTOR).ceil
return sectors
end
@nyx
nyx / dig.rb
Created February 15, 2012 15:14
Patch Ruby's Hash class to simplify access and improve error messages. dig is useful for rummaging through deeply nested hashes
# patch Ruby's Hash class to simplify access and improve error messages
# useful for digging through deeply nested hashes
class Hash
def dig(*keys)
#puts "dig: #{keys.inspect} #{self.inspect}"
if keys.empty?
return self
else
begin
v = self.fetch(keys.first)
@nyx
nyx / inside_out_fib.rb
Created January 10, 2012 22:34
fibonacci sequence with recursion in default argument place (yes I know this is awful)
def fib(a=0, b=1, c=(a > 100 ? exit : (puts a; fib(b, a+b))))
return c
end
fib
@nyx
nyx / .emacs
Created December 10, 2011 18:08
(defun my-visible-bell ()
(set-face-background 'mode-line "red")
(run-at-time "0.1 sec" nil
'(lambda ()
(set-face-background 'mode-line "#e2962f"))))
(setq ring-bell-function #'my-visible-bell)
@nyx
nyx / timeout_error_with_custom_error_message.rb
Created June 21, 2011 18:18
custom messages on exceptions
require 'httparty'
class Rocker
include HTTParty
def initialize(timeout = 1)
self.class.default_timeout(timeout)
end
def rock
self.class.get("http://127.0.0.1:1337/")
@nyx
nyx / no_http_response.js
Created June 21, 2011 17:43
nodejs http server for testing client timeout behavior
var http = require('http');
http.createServer(function (req, res) {
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
@nyx
nyx / first_class_methods.rb
Created March 31, 2011 13:53
grab a method from a class and pass it to Array#map as a block for great justice
class Adder
def initialize(a)
@a = a
end
def add_to(b)
@a + b
end
end
add_four_to = Adder.new(4).method(:add_to)
[1,2,3].map(&add_four_to)
#! /usr/bin/env ruby
require 'yaml'
require 'term/ansicolor'
include Term::ANSIColor
# Usage:
# > rake test | bookmark # => Bookmark test output
# > bookmark 1 # => Open first failing test.
# > bookmark 2 # => Open second failing test.