Skip to content

Instantly share code, notes, and snippets.

View fxn's full-sized avatar

Xavier Noria fxn

View GitHub Profile
@fxn
fxn / gist:1521270
Created December 26, 2011 14:31
Count number of divisors with a regexp
See https://github.com/fxn/math-with-regexps.
Traditionally, System V has returned the error EAGAIN
for a nonblocking I/O operation that cannot be satisfied,
while Berkeley-derived implementations have returned the
error EWOULDBLOCK. Because of this history, the POSIX
specification says either may be returned for this case.
Fortunately, most current systems define these two error
codes to be the same (check your system's <sys/errno.h>
header), so it doesn't matter which one we use. In this
text, we use EWOULDBLOCK.
@fxn
fxn / Action Mailer
Created January 20, 2012 17:20
Ruby on Rails v3.2.0 CHANGELOGs
## Rails 3.2.0 (January 20, 2012) ##
* Upgrade mail version to 2.4.0 *ML*
* Remove Old ActionMailer API *Josh Kalderimis*
@fxn
fxn / Action Mailer
Created January 26, 2012 23:26
Ruby on Rails v3.2.1 CHANGELOGs
## Rails 3.2.1 (January 26, 2012) ##
* No changes.
@fxn
fxn / top_ten_instantiated_classes.rb
Created February 9, 2012 10:32
Top ten instantiated classes by instance count on Ruby 1.9.3 startup (MRI)
classes = Hash.new(0)
ObjectSpace.each_object do |object|
classes[object.class] += 1
end
top_ten = classes.values.uniq.sort.reverse[9]
classes.select {|_, c| c >= top_ten}.sort_by {|_, c| -c}.each do |klass, c|
puts "#{klass}: #{c}"
end
require 'active_support/inflector'
require 'benchmark'
# QUICK HACK
class RuleSet
def initialize
@rules = []
@regexp = nil
end
require 'active_support/inflector'
require 'benchmark'
# QUICK HACK
class RuleSet
def initialize
@rules = []
@regexp = nil
end
require 'active_support/inflector'
require 'benchmark'
# QUICK HACK
class RuleSet
def initialize
@rules = []
@regexp = nil
@group2rule = {}
end
o = Object.new
p o
def o.to_s
:foo
end
puts "#{o}"
@fxn
fxn / atomic_redis_extensions_with_lua.md
Created December 11, 2012 18:55
Atomic Redis Extensions with Lua

Atomic Redis Extensions with Lua

Redis has a very simple execution model: while the server is evented and is able to cope with many concurrent clients, the commands themselves are executed one after the other. Redis is single-threaded. So, you know that while a LPUSH runs, say, no other command can possibly read or change anything at the same time.

If you open a transaction with MULTI, the commands of the transaction are queued, and when the transaction is committed, the queued commands run atomically. No other clients are served while they run because EXEC, as the rest of ordinary commands, is executed in that single thread. Other clients may be served while the commands are queued though, and that gives place to optimistic idioms with WATCH.

Redis 2.6 comes with support for Lua scripting, and that yields new options. because **Lua script