Skip to content

Instantly share code, notes, and snippets.

View jqr's full-sized avatar

Elijah Miller jqr

View GitHub Profile
require "thread"
# This is an example of how to use popen in a nonblocking fashion while
# consuming the output and manipulating it slightly. Additionally it ensures
# that the output maintains some consistency even with interleaving writes.
# Make a mutex so that we only let one thing write to stdout at a time
@stdout_mutex = Mutex.new
# A version of puts which shows some debug information, handy for showing off
def dump_redis(redis, keys)
values = {}
keys.each do |key|
case type = redis.type(key)
when "hash"
values[key] = redis.hgetall(key)
when "set"
values[key] = Set.new(redis.smembers(key))
when "list"
values[key] = redis.lrange(key, 0, -1)
@jqr
jqr / example.rb
Created August 22, 2008 18:21
Ruby String#to_json with support for html encoding and decoding
# When inserting Javascript in HTML there is a a problem with the
# </script> end tag.
#
# Standard Javascript escaping works fine for most strings, but the
# browser interprets </script> before Javascript is being parsed, so
# it needs to be HTML escaped while the browser is parsing, and then
# unescaped as Javascript parses it.
# Regular to_json
{ 'key' => '</script>' }.to_json
# How I might test this helper
module SomeHelper
def title(page_title)
content_for(:title) { page_title }
end
end
describe SomeHelper
include SomeHelper
# http://facets.rubyforge.org/doc/api/core/classes/Array.html#M000145
require 'rubygems'
require 'facets'
class Array
def permutations(min = 0, max = size + 1)
variations = []
(min..max).each do |permutation_size|
permutation(permutation_size) do |permutation|
# Re: http://twitter.com/danielmorrison/status/1149336565
require 'rubygems'
require 'httparty'
require 'hpricot'
url = 'http://rubyonrails.org'
document = Hpricot.parse(HTTParty.get(url))
scrape = {
import os, re
class CachedFileSearch(object):
"""Cached File Search"""
def __init__(self, dir):
self.dir = dir
self.all_files = os.popen('find ' + dir).read()
self.clear_cache()
-- Demonstration of MySQL bug and a work-around.
-- See http://bugs.mysql.com/bug.php?id=36772
-- It is fixed in MySQL 5.0.74
DROP TABLE `test`;
CREATE TABLE `test` (
`id` INT
);
INSERT INTO `test` VALUES
module Enumerable
def group_by_individual
grouped = {}
each do |item|
yield(item).each do |key|
grouped[key] ||= []
grouped[key] << item
end
end
grouped