Skip to content

Instantly share code, notes, and snippets.

View xpepper's full-sized avatar
💭
😏

Pietro Di Bello xpepper

💭
😏
View GitHub Profile
@xpepper
xpepper / capistrano_rails_guide.md
Created March 22, 2012 23:10 — forked from jrochkind/gist:2161449
A Capistrano Rails Guide

A Capistrano Rails Guide

by Jonathan Rochkind, http://bibwild.wordpress.com

why cap?

Capistrano automates pushing out a new version of your application to a deployment location.

I've been writing and deploying Rails apps for a while, but I avoided using Capistrano until recently. I've got a pretty simple one-host deployment, and even though everyone said Capistrano was great, every time I tried to get started I just got snowed under not being able to figure out exactly what I wanted to do, and figured I wasn't having that much trouble doing it "manually".

Continued from I'm learning vim. Here's how I'm doing it, by Chris Geihsler

I've committed to learning vim to become more productive without the crutch of an expensive tool and to decouple myself from a specific OS.

Here's what I've done so far:

@xpepper
xpepper / class_detector.rb
Created April 4, 2012 21:47
How do you tell if a Ruby object is a class?
class Object
def is_a_class?
respond_to? :superclass
end
end
class MyClass
end
@xpepper
xpepper / class_method_lookup.rb
Created April 22, 2012 22:45
Class method lookup
class Parent
class << self
def who_am_i
puts "I'm #{self}"
end
def singleton
class << self; self; end
end
end
@xpepper
xpepper / document.rb
Created May 16, 2012 12:23
Adding autoload feature to a Ruby class
class Document
def self.reload
instance_methods(false).each { |m| remove_method(m) }
class << self
Document.methods(false).reject {|m| m == "reload" }.each do |m|
remove_method(m)
end
end
load(__FILE__)
@xpepper
xpepper / issues_with_modules.md
Created December 1, 2012 20:44 — forked from ryanb/issues_with_modules.md
Points on how modules can make code difficult to read.

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article &lt; ActiveRecord::Base
def search(search_options)
name_regexp = search_options[:name_regexp] || ""
starting_date = search_options[:starting_date] || Date.today
ending_date = search_options[:ending_date] || Date.today
where(:name => /#{name_regexp}/, "efforts.date" => {'$gte' => Date.parse(starting_date)}, "efforts.date" => {'$lte' => Date.parse(ending_date)})
end
@xpepper
xpepper / transactions_in_rails.rb
Created March 6, 2013 23:35
test delle transazioni in Rails
# Ho creato una app rails semplice, con un modello User e uno Order.
# User:
class User < ActiveRecord::Base
attr_accessible :name, :surname
has_many :orders
end
#Order:
class Order < ActiveRecord::Base
#!/usr/bin/env ruby
require 'benchmark'
REGEXPS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
/^Missing API definition file in (.+)$/i,
/^cannot load such file -- (.+)$/i,
]
@xpepper
xpepper / pr.md
Created March 25, 2013 12:03 — forked from piscisaureus/pr.md

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this: