Skip to content

Instantly share code, notes, and snippets.

View EmmanuelOga's full-sized avatar
🕹️

Emmanuel Oga EmmanuelOga

🕹️
View GitHub Profile
# Taken from passenger_memory_stats script
# Returns the private dirty RSS for the given process, in KB.
def determine_private_dirty_rss(pid)
total = 0
File.read("/proc/#{pid}/smaps").split("\n").each do |line|
line =~ /^(Private)_Dirty: +(\d+)/
if $2
total += $2.to_i
end
# Factory girl, relaxed.
#
# Factory.define :user do |f|
# f.login 'johndoe%d' # Sequence.
# f.email '%{login}@example.com' # Interpolate.
# f.password f.password_confirmation('foobar') # Chain.
# end
#
# Factory.define :post do |f|
# f.user { Factory :user } # Blocks, if you must.
# More "proper" than a miniskirt (http://gist.github.com/273579).
class Minidress
@@factories = {}
class << self
def define(name, &block)
@@factories[name.to_s] = block
end
def build(name, attrs = {})
new(name.to_s.classify.constantize.new, &@@factories[name.to_s]).record
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'nokogiri'
class Array
module RegexpIndexing
def [] a
a.is_a?(Regexp) ? select {|b| b.is_a?(String) && b[a]} : super
require 'rubygems'
require 'eventmachine'
require 'em-http' # gem install em-http-request
require 'yajl' # gem install yajl-ruby
module ChatClient
def post_init
@name = "anonymous_#{ChatClient.client_num+=1}"
@sid = ChatClient.channel.subscribe(method(:send_msg))
@buf = ''
# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby
# - a browser with WebSocket support
#
# Usage:
# ruby redis_pubsub_demo.rb
#
@EmmanuelOga
EmmanuelOga / nokogiri.to_hash.rb
Created April 19, 2010 04:38 — forked from dimus/Hash.from_xml using Nokogiri
Convert a nokogiri tree to a Hash
module NokogiriUtils
extend self
# http://gist.github.com/370755
def hash_from_node(node)
{ node.root.name.to_sym => xml_node_to_hash(node.root) }
end
def xml_node_to_hash(node)
return to_value(node.content.to_s) unless node.element?
run "rm public/index.html"
file "Isolate", <<-END
gem "rails", "3.0.0"
gem "haml", "3.0.18"
gem "sqlite3-ruby", "1.3.1"
env :development do
gem "thin", "1.2.7"
gem "haml-rails", "0.2"
var setTextMeasure = function (contentElement, targetMeasure, maxSize, minSize) {
if (!contentElement) contentElement = document.createElement('p');
if (!targetMeasure) targetMeasure = 66;
if (!maxSize) maxSize = 16;
if (!minSize) minSize = 9;
var sizer = contentElement.cloneNode();
sizer.style.cssText = 'margin: 0; padding: 0; color: transparent; background-color: transparent; position: absolute;';
@EmmanuelOga
EmmanuelOga / g.sh
Created January 31, 2012 17:35 — forked from anonymous/g.sh
#!/usr/bin/env ruby
cmd = %Q{curl https://gist.github.com/gists -s -L -o /dev/null -w "%{url_effective} " }
files = ARGV.empty? ? Dir["**/*"] : Dir[*ARGV].uniq
files.select { |f| File.file?(f) }.each_with_index do |path, i|
cmd << %Q{-F "file_ext[gistfile#{i}]=#{File.extname(path)[1..-1]}" }
cmd << %Q{-F "file_name[gistfile#{i}]=#{File.basename(path)}" }
cmd << %Q{-F "file_contents[gistfile#{i}]=<#{path}" }