Skip to content

Instantly share code, notes, and snippets.

@nickstenning
nickstenning / bbc.rb
Created March 25, 2009 16:30
BBC headline scraper. Requires Hpricot.
require 'rubygems'
require 'hpricot'
require 'open-uri'
BBC_URL = 'http://news.bbc.co.uk'
SELECTORS = '.ticker_content_anchor , #other-top-stories .story, #third-story .story, #most-popular .story, #featured-site-top-stories---democracy-live .story, #more-from-bbc-news .story, #featured-site-top-stories---bbc-sport .story, #geo-uk-news-digest li .story, #also-in-the-news .story, #second-story .story, #top-story .story'
bbc = open(BBC_URL)
links = Hpricot(bbc).search(SELECTORS)
Element.implement({
toObject: function () {
var obj = {};
this.getElements('input, select, textarea, button').each(function(el){
if (!el.name || el.disabled) return;
var value = (el.tagName.toLowerCase() == 'select') ? Element.getSelected(el).map(function(opt){
return opt.value;
}) : ((el.type == 'radio' || el.type == 'checkbox') && !el.checked) ? null : el.value;
$splat(value).each(function(val){
if (typeof val !== "undefined") { obj[el.name] = val; }
if (Browser.Features.query) {
Native.implement([Document, Element], {
getElements: function(expression, nocash) {
return new Elements(this.querySelectorAll(expression), {ddup: (expression.length > 1), cash: !nocash});
}
});
}
$(document.body).live('mouseup', function () { console.log("mouseup from elem") });
$('body').live('mouseup', function () { console.log("mouseup from string selector") });
addDelegatedEvent: function (selectorOrElement, event, functionName) {
var __obj = this;
$(this.element).bind(event, function (ev) {
for(var el = ev.target; el !== __obj.element.parentNode; el = el.parentNode) {
if (el === selectorOrElement || $(el).is(selectorOrElement)) {
return __obj[functionName].apply(__obj, arguments);
}
}
});
}
#include "particle.hh"
#include "simulator.hh"
using namespace std;
Particle::Particle(string n, const double m, Point x0, Arrow v0) : name(n), mass(m), x(x0), v(v0)
{}
void Particle::setSimulator(Simulator* sim)
{
J = 1
Mu = 1
if ARGV.length != 2
puts "Usage: #{File.basename(__FILE__)} <T> <H>"
exit 1
else
KbT = ARGV.shift.to_f
H = ARGV.shift.to_f
end
@nickstenning
nickstenning / console.js
Created July 1, 2010 11:50
Javascript console stubber
// Stub the console when not available so that everything still works.
(function () {
var i, functions = ["log", "debug", "info", "warn", "exception", "assert", "dir", "dirxml", "trace", "group", "groupEnd", "groupCollapsed", "time", "timeEnd", "profile", "profileEnd", "count", "clear", "table", "error", "notifyFirebug", "firebug", "userObjects"];
if (!("console" in this)) {
this.console = {};
for (i = 0; i < functions.length; i += 1) {
this.console[functions[i]] = function () {};
}
// Before running this file, do `touch foo` in the same directory.
var fs = require('fs'),
sys = require('sys');
var f = fs.readFileSync('foo', 'utf8');
sys.puts("sync: " + sys.inspect(f));
fs.readFile('foo', 'utf8', function (err, data) {
sys.puts("async: " + sys.inspect(data));
var fs = require('fs'),
sys = require('sys');
fs.writeFileSync('foo-sync', '', 'utf8');
try {
fs.writeFile('foo-async', '', 'utf8', function (err) {
if (err) sys.puts("callback caught: " + sys.inspect(err));
});
} catch(e) {