Skip to content

Instantly share code, notes, and snippets.

class Foo
@@foo = nil
def self.seti( value )
@foo = value
end
def self.setc( value )
@@foo = value
end
def self.geti
@foo
@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];
@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();
var versionByLevelAndDigits = {
L : {
41 : 1,
77 : 2,
127 : 3
},
M : {
34 : 1,
63 : 2,
101 : 3
@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>
@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.
*
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
file = ARGV[0]
2.times do
lines, bytes = 0,0
start = Time.now
contents = File.open(file){ |f| f.read }
contents.scan(/.+/) do |line|
lines += 1
bytes += line.size
end
elapsed = Time.now-start
file = ARGV[0]
2.times do
lines, bytes = 0,0
start = Time.now
File.foreach(file) do |line|
lines += 1
bytes += line.size
end
elapsed = Time.now-start
@Phrogz
Phrogz / phrogz_tweak.rb
Created November 9, 2010 19:52
Temporarily tweak classes to use new methods; idea (and necessary 'remix' functionality) stolen from banister(fiend)
# gem install remix; needed to uninclude modules
require 'remix'
class Class
@tweaks = Hash.new{ |h,k| h[k]=Hash.new{ |h,k| h[k] = Module.new } }
class << self; attr_reader :tweaks; end
def when_tweaked_as(name,&block)
Class.tweaks[name][self].class_eval(&block)
end
def apply_tweak(name,mod=nil)