Skip to content

Instantly share code, notes, and snippets.

View foca's full-sized avatar
👋
Looking for work!

Nicolás Sanguinetti foca

👋
Looking for work!
View GitHub Profile
@foca
foca / state_pattern_with_micromachine.rb
Created October 14, 2010 03:33
How to use the State pattern with MicroMachine, the simplest FSM lib out there :)
# This aims to reproduce the State pattern using MicroMachine.
# We're just implementing the example from http://github.com/dcadenas/state_pattern
require "micromachine"
module Stop
def self.color
"red"
end
@foca
foca / factories.rb
Created November 28, 2010 01:17
All factory libraries are overengineered.
require "ffaker"
class ActiveRecord::Base
def self.define_factory(name=nil, parent=nil, &block)
name = [name, "factory"].compact.join("_")
scope name, lambda { |overrides={}|
parent ||= scoped
attributes = instance_eval(&block).merge(overrides)
parent.where(attributes)
}
if [ -x ~/.bash_completion.d/_transcript ]; then
complete -C ~/.bash_completion.d/_transcript -o default transcript
fi
ruby-1.9.2-rc2 > a = [1, 2, 3]
=> [1, 2, 3]
ruby-1.9.2-rc2 > a[3, 0]
=> []
ruby-1.9.2-rc2 > a[4, 0]
=> nil
@foca
foca / periodic-phrase
Created January 23, 2011 22:42
Simple script to generate combinations of the elements from the periodic table to create strings provided by the user. Very inefficient, and probably buggy, but it works well enough to play with my http://www.thinkgeek.com/interests/giftsunder10/8fc5/ :)
#!/usr/bin/env ruby
abort "You need to provide a string as the last argument" if ARGV.empty?
$debug = true if (ARGV & %w(-d -v --debug --verbose)).any?
def ruler_for(used)
size = used.map(&:size).inject(:+) + used.size - 1
"-" * size
end
@foca
foca / syn
Created January 31, 2011 13:38
Wrapper for synergy to ease the pain of configuring/running
#!/usr/bin/env ruby -w
def ps(command, command_size=3)
capture = (5...5+command_size).map {|c| "$#{c}" }.join(' " " ')
%Q(ps x | grep #{command} | grep -v 'grep #{command}' | awk '{ print #{capture} }')
end
def ssh(server, command)
%Q(ssh #{server} #{command})
@foca
foca / gist:821116
Created February 10, 2011 19:07
This is totally evil.
o1 = Object.new
o2 = Object.new
def (1 > 2 ? o1 : o2).blah
puts "blah"
end
o2.blah #=> puts "blah"
o1.blah #=> NoMethodError
Started by user anonymous
Checkout:workspace / /var/lib/jenkins/jobs/Test/workspace - hudson.remoting.LocalChannel@13d7254
Using strategy: Default
Last Built Revision: Revision 6352919e95820aac6c5fd4a2302231d3ec84ab3f (origin/master)
Checkout:workspace / /var/lib/jenkins/jobs/Test/workspace - hudson.remoting.LocalChannel@13d7254
Fetching changes from the remote Git repository
Fetching upstream changes from git://github.com/foca/moody.git
Seen branch in repository origin/master
Commencing build of Revision 6352919e95820aac6c5fd4a2302231d3ec84ab3f (origin/master)
Checking out Revision 6352919e95820aac6c5fd4a2302231d3ec84ab3f (origin/master)
@foca
foca / gist:867359
Created March 12, 2011 16:46
This is how awesome history.js is -> it just works.
$(window).bind("statechange", function() {
var state = History.getState();
load(state.url, false);
});
function load(url, push) {
if (push === true || push === undefined)
History.pushState(null, null, url);
$.ajax({
# cd into a matching gem directory. Pass a grep compatible
# regular expression. If you don't pass anything, it goes
# to the root gem directory.
cdgem ()
{
local gempath=$(gem env gemdir)/gems;
if [[ $1 == "" ]]; then
cd $gempath;
return;
fi;