Skip to content

Instantly share code, notes, and snippets.

View tkareine's full-sized avatar

Tuomas Kareinen tkareine

View GitHub Profile
@tkareine
tkareine / foo.rb
Created October 14, 2009 07:29
Test Ruby executable with code injection to have stubs/mocks
#!/usr/bin/env ruby
puts "from original executable, args: #{ARGV.join(" ")}"
require "pstore"
module Notes
class << self
attr_accessor :store_location
def put_note(id, note)
store.transaction do
store[:notes] ||= {}
store[:notes][id] = note
#!/usr/bin/env ruby
def exit_upon(*signals)
process_going_down = false
signals.map { |signal| signal.to_s.upcase }.each do |signal|
Signal.trap(signal) do
exit! if process_going_down # terminate if still executing the given block
process_going_down = true
yield(signal) if block_given?
exit! 0
@tkareine
tkareine / count_down_latch.rb
Created December 13, 2010 21:56
A simple count down latch in Ruby
#!/usr/bin/env ruby
# coding: utf-8
require 'thread'
class CountDownLatch
attr_reader :count
def initialize(to)
@count = to.to_i
@tkareine
tkareine / create_random_file.rb
Created January 27, 2011 11:35
Create random plain text file
# coding: utf-8
SIZE = ARGV.shift.to_i
FILENAME = ARGV.shift
if SIZE < 1 || FILENAME.nil?
puts "Usage: ruby #{File.basename(__FILE__)} <size> <file>"
exit 1
end
@tkareine
tkareine / pfork.rb
Created May 31, 2011 22:35
Capture input and output and get the exit status of arbitrary Ruby code, variant of Open4::popen4
# coding: utf-8
module PFork
extend self
# Call function +fun+ in a child process, giving access to the child process
# id and capturing STDIN, STDOUT, and STDERR of the function call.
#
# Adapted from stdlib's Open3::popen3 and
# {Open4::popen4}[https://github.com/ahoward/open4] by Ara T. Howard, but
@tkareine
tkareine / fork_test_demo.rb
Created September 21, 2011 14:05
Test global state changing code with fork
# coding: utf-8
# Test global state changing code with fork
#
# * You should avoid having global state altogether
# * That said, if you cannot avoid it, for one reason or another,
# you can execute state changing code in a child process
# * Fork a child process, execute state changing code there
# * The forked child process has its own memory space, separate
# from the parent process
@tkareine
tkareine / jasmine.beforeAll.js
Created December 15, 2011 13:18
beforeAll decorator for Jasmine
function withBeforeAll(tests) {
var beforeAllCalled = false
var helpers = {
beforeAll: function (fun) {
beforeEach(function () {
if (beforeAllCalled) return
fun()
runs(function () { beforeAllCalled = true } )
})
(ns pi-estimation
"See Opinion 18 in <http://programmers.blogoverflow.com/2012/08/20-controversial-programming-opinions/>")
;; Given that Pi can be estimated using the function 4 * (1 – 1/3 + 1/5
;; – 1/7 + …) with more terms giving greater accuracy, write a function that
;; calculates Pi to an accuracy of 5 decimal places.)
;; with lazy-seq
(defn odds []
var subj0 = new Rx.Subject()
var obs0 = subj0.doAction(function(e) { console.log("obs0 doAction", e) })
var subs0_1 = obs0.subscribe(function(e) { console.log("subs0_1", e) })
subj0.onNext('a')
//> obs0 doAction a
//> subs0_1 a
var subs0_2 = obs0.subscribe(function(e) { console.log("subs0_2", e) })
subj0.onNext('b')