Skip to content

Instantly share code, notes, and snippets.

@benolee
benolee / chatlogger.js
Created August 19, 2011 04:15
writes chat logs to a file
var fs = require('fs');
var redis = require('redis');
var client = redis.createClient();
client.psubscribe('juggernaut');
client.on('pmessage', function(pattern, channel, msg){
var message = JSON.parse(msg);
var data = message.data;
@benolee
benolee / gist:1168339
Created August 24, 2011 15:42
Four ways to figure out which flavor of linux you're on, in order of usefulness
cat /etc/*-release
cat /etc/issue
cat /proc/version
dmesg | head -1
# I want this method in ruby-core
def let
yield
end
def fib(i)
let do |n = 1, result = 0|
if i == -1
result
else
@benolee
benolee / inheritance_test.rb
Created September 27, 2011 15:00
Playing around with inheritance vs. inclusion
require 'minitest/spec'
require 'minitest/autorun'
describe "class inheritance" do
it "inherits public singleton methods" do
superclass = Class.new
superclass.define_singleton_method(:pub_sing_meth) {}
subclass = Class.new(superclass)
subclass.singleton_methods.include?(:pub_sing_meth).must_equal true
end
@benolee
benolee / wut.rb
Created October 4, 2011 16:34
WHAT
W, H = 44, 54
c = 7 + 42 * W
a = [0] * W * H
g = d = 0
f = proc do |n|
a[c] += 1
o = a.map {|z| " :#"[z, 1] * 2 }.join.scan(/.{#{W * 2}}/)
puts "\f" + o.map {|l| l.rstrip }.join("\n")
sleep 0.005
d += 1 - 2 * ((g ^= 1 << n) >> n)
@benolee
benolee / unary_operator_challenge.rb
Created October 13, 2011 20:11
unary operator challenge
# In case you haven't seen +@ before, it works as a unary operator (as does -@)
# Example:
#
# a = A.new
# +a #=> RuntimeError: You must provide a block!
# a.send(:+@, &proc { "hello world!" }) #=> "hello world!"
class A
def +@
if block_given?
yield
@benolee
benolee / gist:1323225
Created October 28, 2011 19:24
CamelCase guard name to find guard class name
diff --git a/lib/guard.rb b/lib/guard.rb
index 368d5aa..2795dc4 100644
--- a/lib/guard.rb
+++ b/lib/guard.rb
@@ -398,10 +398,10 @@ module Guard
def get_guard_class(name)
name = name.to_s
try_require = false
- const_name = name.downcase.gsub('-', '')
+ const_name = name.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
@benolee
benolee / gist:1356120
Created November 10, 2011 20:34
`rvm gemset empty` doesn't seem to work
~$ rvm gemset create mygems
'mygems' gemset created (/home/ben/.rvm/gems/ruby-1.9.2-p290@mygems).
~$ rvm gemset use mygems
~$ rvm gemset empty
/home/ben/.rvm/gems/ruby-1.9.2-p290@ already does not exist.
~$ rvm use 1.9.2
Using /home/ben/.rvm/gems/ruby-1.9.2-p290
~$ rvm gemset empty
/home/ben/.rvm/gems/ruby-1.9.2-p290@ already does not exist.
~$ rvm info
@benolee
benolee / decent_exposure.rb
Created April 10, 2012 13:49
decent_exposure and proffer
# This seems like it does exactly what decent_exposure does...
# app/controllers/people_controller.rb
class PeopleController < ApplicationController
helper_method :people
def index
end
private
@benolee
benolee / gist:2725802
Created May 18, 2012 15:15
showing pry's find-method stuff
$ pry
1.9.3 (main):0 > find-method puts
#<Class:0x000000018326c8>#puts
#<Module:0x00000001743398>#puts
IO
IO#puts
IO::writable
IO::writable#puts