Skip to content

Instantly share code, notes, and snippets.

View jbgutierrez's full-sized avatar

Javier Blanco Gutiérrez jbgutierrez

View GitHub Profile
@jbgutierrez
jbgutierrez / gist:247074
Created December 2, 2009 09:12
Get rid of complex HTML logic in Helpers with Composite Pattern
# Composite Pattern
class TagNode
include ActionView::Helpers::TagHelper
def initialize(name, options = {})
@name = name.to_s
@attributes = options
@children = []
end
@jbgutierrez
jbgutierrez / maze.rb
Created January 12, 2010 18:46
Solution for the Rubylearning Challenge #5 (http://tinyurl.com/yf8lxch)
class String
alias_method :is_a, :==
alias_method :is_an, :==
end
class Maze
NAVIGABLE = ' '
WALL = '#'
POINT_A = 'A'
# Newbie Programmer
def factorial(x)
if x == 0
return 1
else
return x * factorial(x - 1)
end
end
puts factorial(6)
puts factorial(0)
@jbgutierrez
jbgutierrez / gist:308904
Created February 19, 2010 17:01
Sort Delicious bookmarks by times saved
// ==UserScript==
// @name Sort Delicious bookmarks by times saved
// @namespace jbgutierrez.info
// @description Sort Delicious bookmarks by times saved
// @include http://delicious.com/*
// ==/UserScript==
// Add jQuery
var dependencies = ['http://jquery.com/src/jquery-latest.js', 'http://tinysort.sjeiti.com/scripts/jquery.tinysort.js'];
dependencies.forEach(function(dependency) {
@jbgutierrez
jbgutierrez / magic_models.rb
Created March 10, 2010 18:54
Dr. Nic's magic models
require 'rubygems'
ActiveRecord::Base.establish_connection (
:adapter => 'mysql',
:database => 'database',
:username => 'username',
:password => 'password',
:host => 'localhost'
)
module LegacyDatabase
require 'dr_nic_magic_models'
# Drop this file in config/initializers to run your Rails project on Ruby 1.9.
# This is three separate monkey patches -- see comments in code below for the source of each.
# None of them are original to me, I just put them in one file for easily dropping into my Rails projects.
# Also see original sources for pros and cons of each patch. Most notably, the MySQL patch just assumes
# that everything in your database is stored as UTF-8. This was true for me, and there's a good chance it's
# true for you too, in which case this is a quick, practical solution to get you up and running on Ruby 1.9.
#
# Andre Lewis 1/2010
# encoding: utf-8
@jbgutierrez
jbgutierrez / gist:445126
Created June 19, 2010 18:30
Migración de servidor subversion con git
git config --global svn.addAuthorFrom true
git config --global svn.useLogAuthor true
git svn clone NEW_URL new
git svn clone ORIGINAL_URL original
cd original
git push ../new/.git master:refs/heads/original
cd ../new
git filter-branch --msg-filter 'sed -e /^git-svn-id:/d' original
echo `git rev-list original | tail -1` `git rev-list master | tail -1` >> .git/info/grafts
git merge original
@jbgutierrez
jbgutierrez / gist:718934
Created November 28, 2010 13:34
video_test.rb
require 'test_helper'
class VideoTest < ActiveSupport::TestCase
test "should accept only valid urls" do
video = Video.new
assert(video.invalid?) # blank urls are not accepted
invalid_urls = # valid but not accepted
%w{
def tip(msg); puts; puts msg; puts "-"*100; end
#
# 30 Ruby 1.9 Tips, Tricks & Features:
# http://www.igvita.com/2011/02/03/new-ruby-19-features-tips-tricks/
#
tip "Upgrading to Ruby 1.9 is simple: rvm install 1.9.2 && rvm --default 1.9.2"
tip "Ruby 1.9 supports named captures in regular expressions!"
<!DOCTYPE HTML>
<head>
<title>FooBar spaguetti riddle</title>
<meta charset='utf-8'>
<script type="text/javascript" src="http://shjs.sourceforge.net/sh_main.js"></script>
<script src="http://shjs.sourceforge.net/lang/sh_javascript.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="http://shjs.sourceforge.net/css/sh_the.css" type="text/css" media="screen" charset="utf-8">
<style type="text/css" media="screen">