Skip to content

Instantly share code, notes, and snippets.

View hsribei's full-sized avatar

Helder S Ribeiro hsribei

View GitHub Profile
@hsribei
hsribei / findPropertyValue.js
Created November 25, 2012 13:02
Find a property that has a certain value and return its object path starting at the root object parameter
// FIXME: this can very rapidly hit the call stack size limit
function findPropertyValue(obj, value) {
if (typeof obj.seenBefore === "undefined") {
// treat for object graph circularity
obj.seenBefore = true;
for (var key in obj) {
if (obj[key] == value) {
return key;
@hsribei
hsribei / gist:1318423
Created October 27, 2011 00:19
A Recorder/Code generator for FireWatir
Abstract
========
This project aims at allowing users to record normal usage on a
website and then have a script generated that can reproduce such
actions.
This script is generated in Ruby and can be changed and mixed with
existing Ruby code to extend its behaviour, using regular Ruby
variables, loop constructs, etc.
@hsribei
hsribei / gist:1313839
Created October 25, 2011 18:56
Git stash saves your uncommited work on a temporary stack and removes it from the current workspace

Git stash saves your uncommited work on a temporary stack and removes it from the current workspace, allowing you to switch branches without seeing complaints about uncommited changes.

@hsribei
hsribei / gist:1313664
Created October 25, 2011 18:00
It's OK to delete branches after merging them

Deleting branches after merging them into other branches can help unclutter your list of branches. If you use a lot of topic branches for features and don't delete them, pretty soon you'll have a very long list of old, unmaintained branches, and that's going to make it harder for you to find the ones you're currently working on.

You can always get back to old branches anyway. When you merge, a merge commit is created on the branch you merged into. Merge commits have two parents, which are referenced by their SHA1s. So if you need to see the old branch, just check out the SHA1 of the corresponding parent on the merge commit. Since it's referenced in a living branch, it's not gonna get garbage-collected by Git.

The only case where the original SHA1 wouldn't be kept around is if it was a fast-forward merge. To prevent that from happening, you can use the option --no-ff on the git merge command, which will create a merge commit even when a fast forward would be possible.

@hsribei
hsribei / gist:1313618
Created October 25, 2011 17:42
A merged git branch remains accessible even if deleted after being merged.

A merged git branch remains accessible even if deleted after being merged

A merge commit has two parent sha1s, so the old branch would be accessible by its sha1 as one of the parents in the merge commit and thus wouldn't be garbage-collected by Git.

@hsribei
hsribei / gist:748665
Created December 20, 2010 17:17
downcase with accents for latin1 (sort of)
# -*- coding: utf-8 -*-
class String
def downcase_with_accents
return nil if s.nil?
norm = self.downcase
norm.tr!('ÁÉÍÓÚÇ', 'aeiouç')
norm.tr!('ÀÈÌÒÙ', 'aeiou')
norm.tr!('ÄËÏÖÜ', 'aeiou')
norm.tr!('ÂÊÎÔÛ', 'aeiou')
norm.tr!('áéíóú', 'aeiou')
@hsribei
hsribei / debugging_attribution_to_specific_hash_keys.rb
Created August 16, 2010 20:14
How to debug Hash attribution for a specific key (like when you want to debug Rails' env hash)
class Hash
def assign_with_debugger(key, value)
debugger if key == "action_dispatch.request.path_parameters"
self.assign_without_debugger(key, value)
end
alias :assign_without_debugger :[]=
alias :[]= :assign_with_debugger
end
#!/usr/bin/env ruby
require 'rubygems'
require 'mechanize'
require 'ostruct'
require 'open-uri'
require 'hpricot'
require 'yaml'
# fix for screwed up net/http
# found at: http://pw.tech-arts.co.jp/technical/cat57/
@hsribei
hsribei / gist:138159
Created June 30, 2009 13:36
Google Fight
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'open-uri'
def google_fight(queries)
queries.max { |a,b| JSON.load(open("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=#{a}"))["responseData"]["cursor"]["estimatedResultCount"].to_i <=> JSON.load(open("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=#{b}"))["responseData"]["cursor"]["estimatedResultCount"].to_i }
end
if __FILE__ == $0
class AddStatsToDelayedJobs < ActiveRecord::Migration
def self.up
add_column :delayed_jobs, :first_started_at, :datetime
add_column :delayed_jobs, :last_started_at, :datetime
add_column :delayed_jobs, :finished_at, :datetime
end
def self.down
remove_column :delayed_jobs, :finished_at
remove_column :delayed_jobs, :last_started_at