Skip to content

Instantly share code, notes, and snippets.

View altamic's full-sized avatar

Michelangelo Altamore altamic

View GitHub Profile
@igrigorik
igrigorik / webapp.rb
Created November 13, 2010 21:28
Inspired by @JEG2's talk at Rubyconf... Any ruby object, as a webapp! 'Cause we can. :-)
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
#
# I was asked for an example of code that was DRY, but still had an
# instance of Connascence of Algorithm going on.
#
# Consider the following code. I think most people would agree that
# the code is fairly DRY in the sense that there is no duplicated
# code.
#
require 'digest/sha1'
@headius
headius / gist:727684
Created December 3, 2010 23:01
A GIL for JRuby?
~/projects/jruby ➔ # single thread performance
~/projects/jruby ➔ jruby --server bench/bench_threaded_reverse.rb 1
...
concurrency is - 1
started thread 0
Thread 0 running
another 10 in #<Thread:0xc8c7d6 run>
another 10 in #<Thread:0xc8c7d6 run>
another 10 in #<Thread:0xc8c7d6 run>
@ryanb
ryanb / github_tree_slider.js
Created December 6, 2010 17:23
This is how GitHub's new AJAX file browser works.
GitHub.TreeSlider = function () {
if (window.history && window.history.pushState) {
function a() {
if (e.sliding) {
e.sliding = false;
$(".frame-right").hide();
$(".frame-loading:visible").removeClass("frame-loading")
}
}
if (!($("#slider").length == 0 || !GitHub.shouldSlide)) if (!navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
## Yacc is dead - Ruby edition (recognizer only)
# Pretty much a direct port of the recognizer from:
# http://matt.might.net/articles/parsing-with-derivatives/code/dparse.rkt
#
# Requires 1.9
# gem install lazy
require 'lazy'
include Lazy::Methods
desc "a task which creates the database user"
task(:createuser) do
require 'yaml'
require 'erb'
path = File.join(Rails.root, 'config', 'database.yml')
config = YAML::load(ERB.new((IO.read(path))).result)
config = config[Rails.env] || config
adapter = config['adapter']
username = config['username']
password = config['password']
@ahoward
ahoward / countdown.js
Created January 18, 2011 15:56
capturing deferred state with a closure
// a solution to http://blog.abhiomkar.in/2010/07/03/javascript-code-challenge-by-dropbox-team/
//
var countdown = function(num){
for (var i = 0; i <= num; i += 1) {
(function(i){ setTimeout(function(){ alert(num - i); }, i * 1000); })(i);
}
}
countdown(5);
@ryanb
ryanb / application.html.erb
Created January 19, 2011 19:31
Example of very simple password authentication.
<!-- layout file -->
<% if current_user %>
Welcome <%= current_user.username %>. Not you? <%= link_to "Log out", logout_path %>
<% else %>
<%= link_to "Sign up", signup_path %> or <%= link_to "log in", login_path %>.
<% end %>
@ahoward
ahoward / mp3scrape.rb
Created January 31, 2011 16:11
mp3scrape.rb
#! /usr/bin/env ruby
Main {
description <<-____
mp3scrape will scour any url for it's mp3 content - the script mirrors,
never downloading the same file twice. it does not, however, crawl a
website for links, it simple scapes all the songs from a single page.
# I want this method in ruby-core
def let
yield
end
def fib(i)
let do |n = 1, result = 0|
if i == -1
result
else