Skip to content

Instantly share code, notes, and snippets.

View altamic's full-sized avatar

Michelangelo Altamore altamic

View GitHub Profile
@ahoward
ahoward / jq-ajax-post.js
Created March 3, 2011 00:20
posting json with jquery....
var data = {};
var options = {};
options.type = 'POST';
options.url = url;
options.dataType = 'json';
options.cache = false;
options.data = jq.toJSON(data);
options.contentType = 'application/json; charset=utf-8';
options.success = function(){};
module InstanceExec
Code = lambda do
unless Object.new.respond_to?(:instance_exec)
module InstanceExecHelper; end
include InstanceExecHelper
def instance_exec(*args, &block)
begin
old_critical, Thread.critical = Thread.critical, true
n = 0
@jbr
jbr / switch.rb
Created February 12, 2011 11:15
ruby 1.9.2 case-like switch
# In response to:
# http://mlomnicki.com/ruby/tricks-and-quirks/2011/02/10/ruby-tricks2.html
# Ruby 1.9.2 has some neat stuff that lets us make a readable
# alternative case statement that calls each method in turn.
# 1.9.2 features used:
# * hashes are ordered in 1.9.2
# * cool JSON-style hash syntax
# * concise lambda syntax
Net::HTTP.get(URI.parse('http://twitter.com/#!/jcasts/statuses/34647787559718912'))
BEGIN {
require 'net/http'
@igrigorik
igrigorik / ruby-1.9-tips.rb
Created February 3, 2011 17:19
Ruby 1.9 features, tips & tricks you may not know about...
def tip(msg); puts; puts msg; puts "-"*100; end
#
# 30 Ruby 1.9 Tips, Tricks & Features:
# http://www.igvita.com/2011/02/03/new-ruby-19-features-tips-tricks/
#
tip "Upgrading to Ruby 1.9 is simple: rvm install 1.9.2 && rvm --default 1.9.2"
tip "Ruby 1.9 supports named captures in regular expressions!"
# 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
@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.
@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 / 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);
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']