Category Archives: Programming

Ruby: Mixin Template

This is the basic structure I use to create a mixin in Ruby:

# = Mixin Template
# == Usage
# ActionController::Base.send :include, MixinModuleName

module MixinModuleName

def self.included(recipient)
recipient.extend(ClassMethods)
recipient.class_eval do
include InstanceMethods
end
end

module InstanceMethods
end

module ClassMethods
[...]

Git: Creating a Remote Branch (and track it locally)

Required steps to create and track locally a remote branch with git:

# Create new remote branch
git push origin origin:refs/heads/new_branch_name

# Make sure everything is updated
git fetch origin

# Check your branch has been created
git branch -r

# Track a remote branch
git branch –track new_branch_name origin/new_branch_name

# Checkout remote branch
git checkout new_branch_name

Git: Undo commit

Sometimes, when the caffeine no longer works, you can make a mistake on a commit. Either you have second thoughts about your commit message, you just forgot to track new project files, or whatever the reasons are you can always undo your last commit (as long you don’t push it to origin repository):

$ git reset [...]

Gem: Simple Navigation (Navigation menu builder for Ruby on Rails)

I’ve just released my new gem simple_navigation. This gem help’s you to create a navigation menu for your Ruby on Rails application.
Installation
Edit your config/environment.rb to setup simple_navigation gem:

config.gem "mexpolk-simple_navigation",
:lib => "simple_navigation",
:source => "http://gems.github.com"

And from the command line, install the plugin:

rake gems:install

Usage
To create your menus create a new [...]

Ruby: Convert Number to Words (Numerical)

Recently I’ve published my new gem NumberToWords. This plugin/gem will override Ruby’s Numeric class adding a new method called to_words. For now, it only works for Spanish.
Sample usage:

require ‘rubygems’
require ‘number_to_words’
5678.to_words
=> “cinco mil seiscientos setenta y ocho”

Another common usage is for describing currency quantities:

number = 4567.90
=> 4567.9
number.to_words.capitalize << ‘ pesos ‘ << (number.to_s.split(‘.’)[1] || 0).rjust(2,’0′)
=> "Cuatro [...]

GEdit: Snippets for Ruby on Rails

I’ve just created a new GitHub repo with GEdit snippets for Ruby on Rails. Any suggestions and comments are welcome. To install them simply copy the xml files to your ~/.gnome2/gedit/snippets directory.
Refs:
GEdit
GEdit Snippets Plugin

Git: Adding a Ruby on Rails Project to Git

Here’s how to add a recently created Ruby on Rails project to git:
1. Create your new project:

$ rails -d mysql project_name

2. Create some .gitignore empty files so you save the entire structure of your project (git doesn’t include empty folders):

$ touch db/.gitignore lib/.gitignore log/.gitignore tmp/.gitignore vendor/.gitignore

3. Create a new .gitignore file in the root directory [...]