Skip to content

Instantly share code, notes, and snippets.

View thisivan's full-sized avatar

Ivan Torres thisivan

View GitHub Profile
@thisivan
thisivan / common_regexp.rb
Created February 10, 2009 03:28
Commonly used regular expressions in Ruby
# Base64
/^[0-9a-zA-Z+\/]+[=]{0,2}$/.match("SGVsbG8gV29ybGQh")
=> #<MatchData "SGVsbG8gV29ybGQh">
# R.F.C. (Méx)
/[A-Z]{3,4}[0-9]{6}[0-9A-Z]{3}/.match("ICS0707315S4")
=> #<MatchData "ICS0707315S4">
# = Base Template
# Base application template for Innetra
# == Requirements
# Requires innetra-easy_generators (http://github.com/innetra/easy_generators/tree/master) and
# gem to work
root_controller_name = 'home'
if yes?('Install haml and innetra-easy_generators gems (both required)?')
run 'gem sources -a http://gems.github.com'
@thisivan
thisivan / sample_collection_select.rb
Created May 14, 2009 04:56
Creating a collection_select with custom options (E.g. new company)
= form.collection_select :company_id, Company.all + |
[OpenStruct.new(:id => 'new', :name => 'New Company')], :id, :name
@thisivan
thisivan / find_command_samples.sh
Created May 14, 2009 05:04
Some find bash command samples
# Find text in files
find | xargs grep 'text to find' -l
# Find and exec command
find -name '*.bak' -exec rm {} \;
# Find only dirs
find -type d
# Find only files
@thisivan
thisivan / gist:112450
Created May 15, 2009 21:31
Undo recent git commit
# Undo commit
git reset --soft HEAD^
@thisivan
thisivan / gist:112550
Created May 16, 2009 03:04
Multitenant Application (Subdomain)
class ApplicationController < ActionController::Base
before_filter :set_current_account
private
def set_current_account
@current_account =
Account.find_by_subdomain(request.subdomains.last)
end
end
@thisivan
thisivan / gist:114078
Created May 19, 2009 13:13
gsub file
def gsub_file(relative_destination, regexp, *args, &block)
path = destination_path(relative_destination)
content = File.read(path).gsub(regexp, *args, &block)
File.open(path, 'wb') { |file| file.write(content) }
end
@thisivan
thisivan / mixin.rb
Created June 10, 2009 06:19
Sample Mixin
# = Mixin Template
# == Usage
# ActionController::Base.send :include, MixinModuleName
module MixinModuleName
def self.included(recipient)
recipient.extend(ClassMethods)
recipient.class_eval do
include InstanceMethods
@thisivan
thisivan / benchmarks.rb
Created June 23, 2009 17:35
Sample benchmarks on Ruby
require 'benchmark'
puts Benchmark.measure { "a"*1_000_000 }
0.060000 0.000000 0.060000 ( 0.067556)
=> nil
n = 50000
Benchmark.bm do |x|
x.report { for i in 1..n; a = "1"; end }
x.report { n.times do ; a = "1"; end }
@thisivan
thisivan / random_chars.rb
Created June 23, 2009 17:40
Generate random strings in Ruby
# Random chars
chars = ('a'..'z').to_a + ('A'..'Z').to_a
(0...10).collect { chars[Kernel.rand(chars.length)] }.join
=> "nmhhuMrybz"