Skip to content

Instantly share code, notes, and snippets.

View milushov's full-sized avatar
😏
crafting new bugs

roma milushov

😏
crafting new bugs
View GitHub Profile
# Newbie Programmer
def factorial(x)
if x == 0
return 1
else
return x * factorial(x - 1)
end
end
puts factorial(6)
puts factorial(0)
@fcalderan
fcalderan / inception-javascript.js
Created November 2, 2010 09:42
inception explained as a 4 nested javascript closures
/*
* Fabrizio Calderan, twitter @fcalderan, 2010.11.02
* I had an idea: could Inception movie be explained by a few javascript closures
* and variable resolution scope (just for fun)?
*
* Activate javascript console =)
*/
<script>
console.group("inception movie");
@cowboy
cowboy / HEY-YOU.md
Last active April 9, 2024 15:54
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
@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!"
@igrigorik
igrigorik / rack_routes.rb
Created March 29, 2011 04:17
Goliath + http_router.rb
require 'goliath'
require 'http_router'
# https://github.com/joshbuddy/http_router
HttpRouter::Rack.override_rack_builder!
class RackRoutes < Goliath::API
map('/get/:id') do |env|
[200, {'Content-type' => 'text/plain'}, ["My id is #{env['router.params'][:id]}\n"]]
end
@dmitryame
dmitryame / followers association with mongoid twitter style
Created April 2, 2011 20:29
followers association with mongoid twitter style
What is the best way to model user/follower (twitter style) relationships with mongo? Most prominent solution is to embed an array of followers ids in every user object. I'm an idealist, and believe that if we are modeling twitter, we eventually will run into a situation when a user has millions of followers which will blow. The relational many_to_many is not easy to model with mongo, mongo_id tries to offer some implementation of basic associations, but the many_no_many, or has_many through is not there yet and I doubt it ever will be.
My solution is a compromise between relational normalization and "embed everything in one document".
class User
include Mongoid::Document
include Mongoid::Timestamps
# user will have many followerships, each will embedd a follower user,
# the user_id is stored on the followership object
@scottharvey
scottharvey / Backbone.js has_many and belongs_to
Created April 18, 2011 05:20
This snippet allows you to define relationships between you backbone models
$.extend(Backbone.Model.prototype, {
hasMany: function(collectionName, associationColumn, opts) {
var parent = this,
associationName = (collectionName.substr(0, 1).toLowerCase() + collectionName.substr(1)),
options = $.extend({ parentId: 'id' }, opts),
collection;
this[associationName] = function() {
collection = window[collectionName].select(function(child) {
return child.get(associationColumn) == parent.get(options['parentId']);
@bowsersenior
bowsersenior / stooge_loader.rb
Created May 18, 2011 23:18
A demo of YAML anchors, references and nested values
require 'rubygems'
require 'yaml'
# A demonstration of YAML anchors, references and handling of nested values
# For more info, see:
# http://atechie.net/2009/07/merging-hashes-in-yaml-conf-files/
stooges = YAML::load( File.read('stooges.yml') )
# => {
# "default" => {
@jocubeit
jocubeit / active_admin_views_pages_base.rb
Created May 22, 2011 03:57
Override footer content in Active Admin gem
# lib/active_admin_views_pages_base.rb
class ActiveAdmin::Views::Pages::Base < Arbre::HTML::Document
private
# Renders the content for the footer
def build_footer
div :id => "footer" do
para "Copyright &copy; #{Date.today.year.to_s} #{link_to('Example.com', 'http://example.com')}. Powered by #{link_to('Active Admin', 'http://www.activeadmin.info')} #{ActiveAdmin::VERSION}".html_safe
@skanev
skanev / close_hidden_buffers.vim
Created July 6, 2011 20:15
Close all hidden buffers in Vim
command! CloseHiddenBuffers call s:CloseHiddenBuffers()
function! s:CloseHiddenBuffers()
let open_buffers = []
for i in range(tabpagenr('$'))
call extend(open_buffers, tabpagebuflist(i + 1))
endfor
for num in range(1, bufnr("$") + 1)
if buflisted(num) && index(open_buffers, num) == -1