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
  end
  
end
Advertisements
Ruby: Mixin Template

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 mil quinientos sesenta y siete pesos 09/100 M.N."

http://github.com/mexpolk/number_to_words/tree/master

Happy Hacking!

Ruby: Convert Number to Words (Numerical)

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 of your project with the following content:

log/**/*
tmp/**/*
db/schema.rb

Note: If you are using sqlite add db/*.sqlite3 to .gitignore so you do not have the database into the repository (you can db:migrate it whenever you need it). Thanks for your reply Piku.

4. Initialize local repository:

$ git init

5. Add the project files and make que initial import:

$ git add .
$ git commit -a -m "* Initial import"

6. Finally if you have a gitosis server you can add it with the following commands:

$ git remote add origin git@YOUR_SERVER_NAME:project_name.git
$ git push origin master

That’s it, your project sources are now managed by git.

Happy hacking!

Git: Adding a Ruby on Rails Project to Git