Thursday, May 27, 2010

TeleHash

TeleHash is a new wire protocol for exchanging JSON in a real-time and fully decentralized manner, enabling applications to connect directly and participate as servers on the edge of the network. It is designed to efficiently route and distribute small bits of data in order for applications to discover each other directly or in relation to events around piece of shared content. The core benefits of TeleHash over other similar platforms and protocols is that it is both generic (not tied to any specific application or content structures) and is radically decentralized with no servers or points of central control.
It works by sending and receiving very simple small bits of JSON via UDP using an easy routing system based on Kademlia, a proven and popular Distributed Hash Table. Everything within TeleHash is routed based on a generic SHA hash, usually of something specific to an application or something common like a URL.
While it is still young, the protocol and early implementations are evolving quickly and can already be used. Everyone is welcome to start experimenting and get involved in any form.

http://telehash.org/

Cross-Origin Resource Sharing (CORS)

Cross-Origin Resource Sharing (CORS) is a W3C Working Draft that defines how the browser and server must communicate when accessing sources across origins. The basic idea behind CORS is to use custom HTTP headers to allow both the browser and the server to know enough about each other to determine if the request or response should succeed or fail.

http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/

Tuesday, May 18, 2010

Get xpath string expression of a document element

XPath is one of those things you don’t hear too much about these days. In the days when XML ruled, XPath was very important to developers as a means of random access within a large structure. Since JSON was popularized, XPath has gotten less and less attention, but there is still fairly good support for XPath queries in browsers.
function getElementXPath(elt)
{
     var path = "";
     for (; elt && elt.nodeType == 1; elt = elt.parentNode)
     {
    idx = getElementIdx(elt);
 xname = elt.tagName;
 if (idx > 1) xname += "[" + idx + "]";
 path = "/" + xname + path;
     }
 
     return path; 
}

function getElementIdx(elt)
{
    var count = 1;
    for (var sib = elt.previousSibling; sib ; sib = sib.previousSibling)
    {
        if(sib.nodeType == 1 && sib.tagName == elt.tagName) count++
    }
    
    return count;
}
http://www.nczonline.net/blog/2009/03/17/xpath-in-javascript-part-1/
http://snippets.dzone.com/posts/show/3754

Sunday, May 9, 2010

Awesome Print

awesome_print is a Ruby tool that provides "pretty printing" support for your objects. It's a bit like p, pp or, if you prefer, puts obj.inspect, but with significantly improved, contextual, colored output. Its creator and maintainer is Michael Dvorkin of Fat Free CRM fame.
Being able to see "inside" Ruby objects on the fly can prove useful whether you're debugging some code your tests did not dare reach or you're just playing around in irb. The most common way to examine objects is with p or the inspect method, but these don't format their output in a particularly easy-to-read way. pp - part of the standard library - is a pretty printer that improves matters but it still leaves a lot to be desired. awesome_print takes it all to the next level.
A visual comparison between pp and awesome_print proves compelling:

awesome_print's most compelling features are showing you the index of array elements within its output, inheritance for the classes of embedded objects, and color coding. Further, it's highly customizable, with the ability to set indent levels as well as the colors for each type of data shown.
To get up and running, gem install awesome_print and then require 'ap' and use ap in place on anywhere you'd usually use p. Yep, that's it.

http://github.com/michaeldv/awesome_print
http://www.rubyinside.com/awesome_print-a-new-pretty-printer-for-your-ruby-objects-3208.html

Wednesday, May 5, 2010

RGraph: HTML5 canvas graph library based on the HTML5 canvas tag

Great Graphs, Charts & Tables That Build Real-Life Math Skills: High-Interest Reproducible Activities That Give Kids Practice Interpreting and Creating Bar Graphs, Line Graphs, Pie Charts, and MoreThis library uses HTML5 features which are implemented in recent browsers. As such you'll need to be using one of these latest browsers: Firefox 3.5+, Chrome 2+, Safari 4+, Opera 10.5+ or Internet Explorer 8.

Microsoft Internet Explorer 8 is now (December 2009) supported in a limited fashion. You can read more about it here.

Older versions of Opera and other older browsers are supported in a limited fashion. If they don't support text or shadows these will naturally be unavailable.

RGraph is a HTML5 canvas graph library. It uses features that became available in HTML5 (specifically, the CANVAS tag) to produce a wide variety of graph types: bar chart, bi-polar chart (also known as an age frequency chart), donut chart, funnel chart, gantt chart, horizontal bar chart, LED display, line graph, meter, odometer, pie chart, progress bar, rose chart, scatter graph and traditional radar chart. RGraph is covered by the RGraph License.

http://www.rgraph.net/

Sunday, May 2, 2010

Find duplicates in an Array with grep

The following example finds duplicates in an array by selecting all array items which have apear more than once.
a = %w(a b a c)
a.uniq.select {|x| a.grep(x).length > 1}
#=> ["a"]

a = %w(a b a c d d d e f g h f)
a.uniq.select {|x| a.grep(x).length > 1}
#=> ["a", "d", "f"]
 
a = %w(a b a c d d d e f g h f)
a.group_by(&:to_s).select{|k,v| v.length > 1}
#=> {"a"=>["a", "a"], "d"=>["d", "d", "d"], "f"=>["f", "f"]}
http://snippets.dzone.com/posts/show/11219