Skip to content

Instantly share code, notes, and snippets.

View rjungemann's full-sized avatar

Roger Jungemann rjungemann

View GitHub Profile
@rjungemann
rjungemann / grok.rb
Created October 3, 2009 07:34
The Object#grok method, for exploring Ruby objects
# the grok method - shows a list of "interesting" methods for an object
# usage - "hello".grok
# => ["%", "*", "+", "<<", "[]", "[]=", "all?", "any?", ...]
class Object
def grok
(self.methods - Object.new.methods).sort
end
end
@rjungemann
rjungemann / tap.rb
Created December 26, 2009 05:54
Enable Object#tap in Ruby 1.8
# Object.tap method for Ruby 1.8 as defined at
# http://ciaranm.wordpress.com/2008/11/30/recursive-lambdas-in-ruby-using-objecttap/
if not Object.respond_to? :tap
class Object
def tap
yield self
self
end
end
end
@rjungemann
rjungemann / SingletonExample.as
Created December 26, 2009 06:09
An example of the Singleton pattern in ActionScript
/*
This code comes from http://www.darronschall.com/weblog/2007/11/actionscript-3-singleton-redux.cfm
*/
// in SingletonExample.as
package com.thefifthcircuit.util {
public class SingletonExample {
public var foo:String;
private static const inst:SingletonExample = new SingletonExample(SingletonLock);
@rjungemann
rjungemann / bayes.rb
Created December 30, 2009 19:01
How to use the Remember gem with Classifier
# An example showing how to use Remember (http://github.com/jpignata/remember)
# to serialize a bayes classifier made with the "classifier" gem. This would
# be useful to share a Bayes classifier across server instances.
#
# This seems to be similar, but more rudimentary, to functionality found in
# Maglev and jruby-maglev, the ability to share globals across interpreters.
require 'rubygems'
require 'xattr'
require 'moneta'
@rjungemann
rjungemann / gist:272676
Created January 9, 2010 02:40 — forked from headius/gist:271764
headius' example of installing gems from maven files
~/projects/jruby ➔ gem install com.lowagie.itext-rtf
Successfully installed bouncycastle.bcmail-jdk14-138-java
Successfully installed bouncycastle.bcprov-jdk14-138-java
Successfully installed bouncycastle.bctsp-jdk14-138-java
Successfully installed com.lowagie.itext-rtf-2.1.7-java
4 gems installed
Installing ri documentation for bouncycastle.bcmail-jdk14-138-java...
Installing ri documentation for bouncycastle.bcprov-jdk14-138-java...
Installing ri documentation for bouncycastle.bctsp-jdk14-138-java...
Installing ri documentation for com.lowagie.itext-rtf-2.1.7-java...
@rjungemann
rjungemann / to_rows.rb
Created January 12, 2010 09:43
Split a Ruby array into regularly-sized chunks
class Array
def to_rows(column_length, padding = true)
i = 0
array = []
while i < self.size
chunk = self[i..(i + column_length - 1)]
chunk << [] while chunk.size < column_length if padding
array << chunk
@rjungemann
rjungemann / Nancy.as
Created January 14, 2010 02:45
Access web services from AS in a Sinatra-like way
package com.thefifthcircuit.nancy {
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
import flash.events.Event;
import flash.events.IOErrorEvent;
public class Nancy {
private var host: String;
@rjungemann
rjungemann / blank.rb
Created January 14, 2010 23:11
Method for Ruby objects to see if they're nil or empty
class Object
def blank?
self.nil? || (self.respond_to?(:empty?) && self.empty?)
end
end
@rjungemann
rjungemann / SocketSample.as
Created January 14, 2010 23:27
Communicate between Flash and Ruby over sockets
package {
import flash.display.Sprite;
import flash.net.Socket;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
public class SocketSample extends Sprite {
private var socket:Socket;
@rjungemann
rjungemann / SocketAMFExample.as
Created January 15, 2010 00:45
An example showing AMF over sockets b/w AS and Ruby
package {
import flash.display.Sprite;
import flash.net.Socket;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
public class SocketAMFExample extends Sprite {
private var socket:Socket;