Skip to content

Instantly share code, notes, and snippets.

View felixyz's full-sized avatar

Felix Holmgren felixyz

View GitHub Profile
@felixyz
felixyz / pretty-date.rb
Created September 17, 2012 21:23 — forked from rjz/pretty-date.rb
Pretty Date in Ruby
# Nothin' special here: just Resig's pretty date function ported to Ruby
# http://ejohn.org/blog/javascript-pretty-date/
def pretty_date(stamp)
now = Time.new
diff = now - stamp
day_diff = ((now - stamp) / 86400).floor
day_diff == 0 && (
diff < 60 && "just now" ||
@felixyz
felixyz / crash_burn_symbols.rb
Last active December 13, 2015 17:38
Crash caused by archiving/dearchiving symbols in RubyMotion. I can't get an interesting stack trace out of this, and NSZombiesEnabled doesn't give me anything either, but the original bug that set me hunting did result in "-[Symbol methodSignatureForSelector:]: message sent to deallocated instance" (with zombies turned on). It doesn't seem to me…
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@stuff = []
NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "crash", userInfo:nil, repeats: true)
NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "burn", userInfo:nil, repeats: true)
true
end
def crash
puts "crash"
@felixyz
felixyz / gist:5679768
Created May 30, 2013 17:57
Use Queue to keep synchronous interface while performing work in parallel. Queue is thread-safe, and extremely easy to use!
require 'thread'
def provider()
threads = []
queue = Queue.new
1.upto(10).each do |n|
threads << Thread.new {
sleep rand(0.1..2)
queue << n
}
@felixyz
felixyz / celluloid_redis_bug.rb
Last active December 18, 2015 10:59
This illustrates celluloid-redis issue #9: https://github.com/celluloid/celluloid-redis/issues/9 A repo containing the below code and branches with variations that variously provoke the same bug or not can be found here: https://github.com/Felixyz/celluloid-redis-bug
require 'thread'
require 'celluloid/io'
require 'celluloid/redis'
KEY = "magic_key"
VALUE = {a: 1, b:2}
class RedisActor
include Celluloid::IO
@felixyz
felixyz / gist:5788285
Created June 15, 2013 14:15
Creating and running >120 Celluloid::IO actors raises an error on MRI, even when doing NO I/O
# Running on OS X
# JRuby + Celluloid = OK
# MRI + Celluloid = OK
# JRuby + Celluloid::IO = OK
# MRI + Celluloid::IO = Too many open files - pipe (Errno::EMFILE)
require 'celluloid/io'
class SimpleActor
# include Celluloid # Creating regular Celluloid actors works on MRI
@felixyz
felixyz / gist:5788375
Created June 15, 2013 14:44
MRI pipe limit (on OS X). This script fails on my Mac after creating 125 x 2 pipe endpoints, raising "Too many open files (Errno::EMFILE)" This is the cause of the problem illustrated here: https://gist.github.com/Felixyz/5788285
wakeups = []
wakers = []
puts "Creating pipes..."
200.times do |n|
puts "[#{n}]"
wakeup, waker = IO.pipe
wakeups << wakeup
wakers << waker
@felixyz
felixyz / item_insert_benchmark.rb
Created September 16, 2013 15:40
Compares speed of two different methods of inserting items in an array.
require 'benchmark'
def insert_then_sort(results, missing_items)
missing_items.each do |id|
results << {id: id, val: nil}
end
results.sort_by!{|item| item[:id] }
results
end
@felixyz
felixyz / sidekiq_unique_jobs_bug_spec.rb
Created February 3, 2014 09:43
RSpec script attempting (and failing) to reproduce sidekiq-unique-jobs issue #27. https://github.com/mhenrixon/sidekiq-unique-jobs/issues/27
require 'spec_helper'
require 'sidekiq'
require 'sidekiq/middleware/chain'
require 'sidekiq/fetch'
require 'sidekiq/processor'
require 'redis/namespace'
redis_url = 'redis://localhost/15'
client = Redis.connect(:url => redis_url)
@felixyz
felixyz / Intro to Common Table Expressions.md
Last active December 22, 2023 12:23
Introduction to transitive closure / Common Table Expressions / iterative queries in SQL

Teh Social Netswork!

CREATE TABLE users (
 id SERIAL PRIMARY KEY,
 name VARCHAR(10) NOT NULL
);

INSERT into users VALUES

@felixyz
felixyz / runflora log
Last active August 29, 2015 14:11
Flora-2 failures on OSX
$ ./flora2/runflora
% Specialising partially instantiated calls to flora_assert_directories/1
[Compiling Foreign Module /Users/felix/Documents/code/flora2/flora2bundle/flora2/cc/prolog2hilog (Prolog compiler options are ignored)]
[prolog2hilog compiled, cpu time used: 0.0010 seconds]
[Compiling C file /Users/felix/Documents/code/flora2/flora2bundle/flora2/cc/prolog2hilog.c using gcc]
/Users/felix/Documents/code/flora2/flora2bundle/flora2/cc/prolog2hilog.c:365:9: warning: comparison of 0 > unsigned expression is always false [-Wtautological-compare]
if (0 > length_diff) return FALSE;
~ ^ ~~~~~~~~~~~
1 warning generated.
gcc -dynamiclib -undefined dynamic_lookup -fPIC -o /Users/felix/Documents/code/flora2/flora2bundle/flora2/cc/prolog2hilog.dylib /Users/felix/Documents/code/flora2/flora2bundle/flora2/cc/prolog2hilog.c -Wall -fPIC -I/Users/felix/Documents/code/flora2/flora2bundle/XSB/emu -I/Users/felix/Documents/code/flora2/flora2bundle/XSB/config/i386-apple-darwin13.2.0 -O3 -fno-strict-aliasing -