Skip to content

Instantly share code, notes, and snippets.

file = ARGV[0]
2.times do
lines, bytes = 0,0
start = Time.now
contents = File.open(file,'rb'){ |f| f.read }
contents.scan(/.+/) do |line|
lines += 1
bytes += line.size
end
elapsed = Time.now-start
@Phrogz
Phrogz / jquery.flot.stack.reversible.js
Created November 23, 2010 20:33
Hack to the jQuery Flot 'stack' plug-in, allowing the user to make bar graphs stack in the same order as the legend.
function stackData(plot, s, datapoints) {
if (s.stack == null)
return;
/********************************************************************************************
* series:{ reverseStack:true } causes the first series to be stacked at the top,
* the last series at the bottom. (Vertical ordering matches the legend.)
*
* Does not properly support excluding specific series from stacking; it's all or none.
*
@Phrogz
Phrogz / nokogiri_reorder_nodes.rb
Created December 17, 2010 16:43
Reordering nodes in a Nokogiri XML document and getting the result as new XML
require 'nokogiri'
xml = <<ENDXML
<top>
<node1>
<value>mmm</value>
<value>zzz</value>
<value>ccc</value>
</node1>
<anothernode>
<value>zzz</value>
var versionByLevelAndDigits = {
L : {
41 : 1,
77 : 2,
127 : 3
},
M : {
34 : 1,
63 : 2,
101 : 3
@Phrogz
Phrogz / SVG Path to Polygon.js
Created February 27, 2011 04:28
Convert a SVG path to a Polygon by sampling the path, but also including all control points in the result. See http://phrogz.net/SVG/convert_path_to_polygon.xhtml
// http://phrogz.net/SVG/convert_path_to_polygon.xhtml
function pathToPolygon(path,samples){
if (!samples) samples = 0;
var doc = path.ownerDocument;
var poly = doc.createElementNS('http://www.w3.org/2000/svg','polygon');
// Put all path segments in a queue
for (var segs=[],s=path.pathSegList,i=s.numberOfItems-1;i>=0;--i) segs[i] = s.getItem(i);
var segments = segs.concat();
@Phrogz
Phrogz / shared variable.js
Created February 28, 2011 21:59
Techniques for sharing a variable between two public functions without exposing the variable.
// Technique 1 - local captures
var _xy = (function(){
var shared = {};
var foo = function(){ ...access shared... };
var bar = function(){ ...access shared... };
return [foo,bar];
})();
var foo = _xy[0];
var bar = _xy[1];
class Foo
@@foo = nil
def self.seti( value )
@foo = value
end
def self.setc( value )
@@foo = value
end
def self.geti
@foo
@Phrogz
Phrogz / simulated_annealing.rb
Created March 24, 2011 16:49
A generic module for performing simulated annealing optimizations
# Include this in a class and define your own #variation method that
# makes returns a new instance that might be better.
#
# Use #add_annealing_constraint to add constraints
module SimulatedAnnealing
def self.included(base) base.extend(ClassMethods) end
module ClassMethods
def annealing_constraints
@annealing_constraints ||= {}
// From SMF
// Replaces the currently selected text with the passed text.
function replaceText(text, textarea)
{
// Attempt to create a text range (IE).
if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
{
var caretPos = textarea.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
@Phrogz
Phrogz / gist:1142428
Created August 12, 2011 16:40
Codepage 65001 prevents psql.exe from working
C:\>"\Program Files\PostgreSQL\9.0\bin\psql.exe" -U postgres -p 5434 uxt
psql (9.0.4)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
uxt=# \dt
List of relations
Schema | Name | Type | Owner