Skip to content

Instantly share code, notes, and snippets.

@peterc
peterc / methods_returning.rb
Last active October 29, 2023 03:10
Object#methods_returning - to work out which method on an object returns what we want
require 'stringio'
require 'timeout'
class Object
def methods_returning(expected, *args, &blk)
old_stdout = $>
$> = StringIO.new
methods.select do |meth|
Timeout::timeout(1) { dup.public_send(meth, *args, &blk) == expected rescue false } rescue false
@gasman
gasman / pnginator.rb
Created April 30, 2012 18:08
pnginator: pack Javascript into a self-extracting PNG
#!/usr/bin/env ruby -w
# pnginator.rb: pack a .js file into a PNG image with an HTML payload;
# when saved with an .html extension and opened in a browser, the HTML extracts and executes
# the javascript.
# Usage: ruby pnginator.rb input.js output.png.html
# By Gasman <http://matt.west.co.tt/>
# from an original idea by Daeken: http://daeken.com/superpacking-js-demos
@ehamberg
ehamberg / gist:1875967
Created February 21, 2012 11:23
C Pointer Declarations

C/C++ Pointer Declaration Syntax – It makes sense!

I never really liked the way pointers are declared in C/C++:

int *a, *b, *c; // a, b and c are pointers to int

The reason is that I am used to reading variable declarations as MyType myVar1, myVar2, myVar3; and I always read “int*” as the type “integer pointer”�. I therefore wanted the following

int* a, b, c; // a is a pointer to int, b and c are ints
@joneskoo
joneskoo / gist-backup.py
Created December 15, 2011 06:13
Clone all my gists automatically (gist backup?)
#!/usr/bin/env python
# Git clone all my gists
import json
import urllib
from subprocess import call
from urllib import urlopen
import os
USER = os.environ['USER']
require 'source_annotation_extractor'
class SourceAnnotationExtractor
# Override the directories that the SourceAnnotationExtractor
# traverses when you call rake notes
def find(dirs=%w(app lib spec public/javascripts))
dirs.inject({}) { |h, dir| h.update(find_in(dir)) }
end
# Override to extract annotations from .haml and .js files
@lancejpollard
lancejpollard / Rails Nested Forms with belongs_to
Created January 13, 2010 23:09
Rails Nested Forms with belongs_to, doesn't work by default
# user.rb
class User < ActiveRecord::Base
belongs_to :address
accepts_nested_attributes_for :address # can't have allow_destroy
end
# users_controller.rb
class UsersController < ApplicationController