Skip to content

Instantly share code, notes, and snippets.

View catwell's full-sized avatar

Pierre Chapuis catwell

View GitHub Profile
@catwell
catwell / ensure-trap.rb
Created November 3, 2010 11:11
In Ruby ensure blocks are *not* executed when an exception is rescued
a = 0
begin
a += 1
raise if a < 3
rescue
puts "rescued"
retry
ensure
puts "ensured"
end

@herberts asked me why I had to leave Télécom Bretagne for Cranfield University to learn about scalability, and I couldn't answer that in 140 chars.

First when I say I left, I just mean I took a double degree during my 3rd year, and I didn't have a choice anyway since school policy makes it mandatory to spend at least a semester abroad. Also, do not misunderstand me: I love the school, I chose to go there instead of "better ranked" schools for a reason. The location is great, and school life too (I couldn't say the same about Cranfield).

As for lessons, I am happy about what I learnt in most department. My problem is with Computer Science & Software Engineering, and it is probably not specific to Télécom Bretagne but the same in most French engineering schools. French engineering students spend their first two years of higher education learning general stuff so most students who enter Télécom Bretagne know nothing at all about CS. The way the curriculum is made, they will have CS classes for at most 3 semes

@catwell
catwell / lua-calls-c
Created January 20, 2011 08:23
From Sean Conner @ Lua ML
Creates a table, fills it with a few values, the current working directory, the current PID and the load average (an array); the load average has a metatable controlling how it's printed.
[spc]lucy:/tmp>gcc -g -shared -fPIC -o silly.so silly.c
[spc]lucy:/tmp>lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
require "silly"
x = silly.info()
print(x.cwd,x.pid,x.loadave)
/tmp 22647 (0.06 0.04 0.01)
os.exit()
@catwell
catwell / watch.rb
Created February 25, 2011 16:02
Redis: WATCH use case
require "redis"
R = Redis.new
def watching(*kk)
5.times do
R::watch(*kk)
begin
r = yield
rescue Object => e
R::unwatch
@catwell
catwell / redis-manifesto.redis
Created March 1, 2011 15:32
Redis meta-Manifesto
redis> sismember DSLs Redis
(integer) 1
redis> zrank priorities memory
(integer) 0
redis> hget reasons api
"data structures"
redis> get code
"print STDOUT q; Just another Perl hacker,; unless $spring"
redis> sinter good complexity
(empty list or set)
@catwell
catwell / great-software.md
Created March 16, 2011 17:56
Great Software

Great Software

This is a list of software that I actually like so much that it doesn't make me want to write my own alternative. This is a rare property which usually means that the software doesn't suck too much.

Note: I will not include common Unix tools. Some of them are great too but you probably already knew that.

  • dtach
  • git
  • lua
  • msgpack
@catwell
catwell / redis-trib-info_string.rb
Created April 13, 2011 16:12
Let's try to refactor info_string, keeping it functional and short
# eg.: @slots = Hash[[5,19,20,32,21,28].zip([:new]*6)]
def info_string
"#{self.to_s.ljust(25)} slots:" + @slots.keys.sort.reduce([]){|a,b|
if a.empty? || !(a[-1][1]+1 == b)
a << [b,b]
else
a[-1][1] = b
a
end
}.map{|x| (x[0] == x[1]) ? x[1] : "#{x[0]}-#{x[1]}"}.join(",")
@catwell
catwell / Set.rb
Created April 15, 2011 08:55
WTF Ruby?
irb(main):095:0> x == y
=> true
irb(main):096:0> cur_users.include?(x)
=> false
irb(main):097:0> cur_users.include?(y)
=> true
irb(main):098:0> cur_users.class
=> Set
@catwell
catwell / moodstocks-rubytoolbox.md
Created April 21, 2011 14:52
Moodstocks' Ruby Toolbox

Moodstocks' Ruby Toolkit

"Rising stars" have a (*), "Fading" have a (-)

ActiveRecord Extensions

  • annotate-models (-)

Authentication

@catwell
catwell / sredis-scripting.rb
Created May 2, 2011 09:24
Redis scripting MHSET benchmark
require "rubygems"
require "redis"
def time(x)
t0 = Time.now
yield
puts "#{x} - #{1000 * (Time.now - t0)}"
end
L = 1.upto(10000.to_i).to_a