Skip to content

Instantly share code, notes, and snippets.

@avdi
avdi / match_method.rb
Created December 6, 2011 21:27
Defining method_missing and respond_to? in one fell swoop
# Do you ever define #method_missing and forget #respond_to? I sure
# do. It would be nice if we could do them both at the same time.
module MatchMethodMacros
def match_method(matcher, &method_body)
mod = Module.new do
define_method(:method_missing) do |method_name, *args|
if matcher === method_name.to_s
instance_exec(method_name, *args, &method_body)
else
@copiousfreetime
copiousfreetime / memstats.rb
Created November 15, 2011 03:06
useful info from /proc/[pid]/smaps
#!/usr/bin/env ruby
#------------------------------------------------------------------------------
# Aggregate Print useful information from /proc/[pid]/smaps
#
# pss - Roughly the amount of memory that is "really" being used by the pid
# swap - Amount of swap this process is currently using
#
# Reference:
# http://www.mjmwired.net/kernel/Documentation/filesystems/proc.txt#361
@fabianp
fabianp / gist:1342033
Created November 5, 2011 21:18
Low rank approximation for the lena image
"""
Low rank approximation for the lena image
"""
import numpy as np
import scipy as sp
from scipy import linalg
import pylab as pl
X = sp.lena().astype(np.float)
pl.gray()
@ahammar
ahammar / DesugarDo.hs
Created November 5, 2011 13:37
Naive do-notation desugarer
import Data.Generics
import Language.Haskell.Parser
import Language.Haskell.Pretty
import Language.Haskell.Syntax
main = do
input <- getContents
case parseModule input of
ParseOk mod -> putStrLn $ prettyPrint $ everywhere (mkT desugarExp) mod
ParseFailed loc msg -> failed loc msg
@nathanmarz
nathanmarz / gist:1246228
Created September 27, 2011 20:56
Exception cause predicate
;; Determine if any of the causes of the exception was of the specified type
(defn exception-cause? [klass ^Throwable t]
(->> (iterate #(.getCause ^Throwable %) t)
(take-while identity)
(some (partial instance? klass))
boolean))
@nathanmarz
nathanmarz / gist:1228302
Created September 20, 2011 04:01
Example of defining a topology in Clojure
(use 'backtype.storm.clojure)
(use 'backtype.storm.config)
(require '[backtype.storm [thrift :as thrift]])
(import 'storm.starter.spout.RandomSentenceSpout)
(import 'backtype.storm.LocalCluster)
(defboltfull suffix-bolt ["word"]
:params [suffix]
:let [conf-state (atom nil)]
:prepare ([conf context collector]
@raggi
raggi / fiber_race.rb
Created September 15, 2011 23:34
An example race condition using fibers
## SCROLL DOWN FOR THE RACE, THE FOLLOWING IS JUST UTILITY FROM COMMONLY USED CODE.
# Author:: Mohammad A. Ali (mailto:[email protected])
# Copyright:: Copyright (c) 2008 eSpace, Inc.
# License:: Distributes under the same terms as Ruby
require 'fiber'
class Fiber
@ryanb
ryanb / spec_helper.rb
Created September 12, 2011 21:37
Use RSpec tags to add behavior around specs.
# Add this to your spec_helper.rb
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.around(:each, :vcr) do |example|
name = example.metadata[:full_description].downcase.gsub(/\W+/, "_").split("_", 2).join("/")
VCR.use_cassette(name, :record => :new_episodes) do
example.call
end
end
end
# https://gist.github.com/1185131
class Array
def next; self[1..-1] end
# returns a function that is the combination of a sequence of function calls
# on some external arguments. the functions combined are the elements of self.
def compose
lambda do |*args|
inject(*args) do |mem, fn|
@mjackson
mjackson / blocks.rb
Created August 11, 2011 18:30
Demonstrates the difference between Ruby's two different block styles.
def a(*args, &block)
puts "a got a block" if block_given?
end
def b(*args, &block)
puts "b got a block" if block_given?
end
# In this call, `b' is called with the block and the
# return value is given to `a' as an argument.