Skip to content

Instantly share code, notes, and snippets.

View afcapel's full-sized avatar

Alberto Fernández-Capel afcapel

View GitHub Profile
#
# This class represents a path in the tree. It stores the positions along the x axis
# as a function of the depth and the sum of the leaves along the path
#
class Path
attr_accessor :path
attr_accessor :sum
def initialize(parent, xpos, value)
if parent
##
# A sitemap has an URL, and holds a collection of pages to be validated
#
module W3Clove
require 'open-uri'
require 'nokogiri'
class Sitemap
attr_accessor :url
@afcapel
afcapel / array.c
Created April 14, 2011 21:02
Snippet from array.c in ruby-1.9.2-p136
/*
* call-seq:
* ary[index] -> obj or nil
* ary[start, length] -> new_ary or nil
* ary[range] -> new_ary or nil
* ary.slice(index) -> obj or nil
* ary.slice(start, length) -> new_ary or nil
* ary.slice(range) -> new_ary or nil
*
* Element Reference---Returns the element at _index_,
@afcapel
afcapel / Benchmark
Created November 16, 2011 22:49
Remove duplicates benchmark
require 'benchmark'
ary = 1000.times.collect { rand(2000) }
Benchmark.bm(10) do |x|
x.report { ary.select { |e| ary.count(e) > 1 }.uniq }
x.report { ary.group_by {|e| e}.select { |k, v| v.size > 1 }.map(&:first) }
x.report { ary.uniq.select { |i| ary.count(i) > 1 } }
x.report { ary.uniq.select { |i| ary.delete_at(ary.index(i)) && ary.delete_at(ary.index(i) || ary.length) } }
x.report { ary.uniq.delete_if { |i| ary.one? { |elm| elm == i } } }
@afcapel
afcapel / def.rb
Created March 24, 2012 21:20
def behaviour
# In Ruby you can use def anywhere in your code, but its behavior
# depends on the context in which it is used.
# Outside a class definition def creates singleton methods
foo = Object.new
# This will create a singleton method in FOO
def foo.speak
puts "foo"
@afcapel
afcapel / gist:3836513
Created October 4, 2012 21:16
Encoding text to ASCII with custom fallbacks
# encoding: utf-8
substitutions = Proc.new do |c|
case c
when 'ñ' then 'n'
when 'í' then 'i'
else '?'
end
end
@afcapel
afcapel / how-the-web-works.md
Last active November 15, 2016 21:23
Article for the amazing practicing ruby Tech Writing Clinic. http://elmcitycraftworks.org/post/36678454423/technical-writing-clinic-winter-2013 One assignment was to write an article explaining a technical concept to non technical readers. Here is my article.

My job, explained so my mom can understand it

"I am a freelance Ruby and Rails developer". When I talk with my geek friends, everyone understands what I do and they have a reasonable understanding of how I spend my day. But sometimes, less technical people ask me what I do for a living and then I tend to dumb down the answer: "I am a programmer, I usually build websites.". Oh -they say- my nephew also make websites, he even built me a blog in Blogger.

Err... yes, but that hardly has anything to do with what I do for a living.

There are many different kinds of websites, built with different technologies, and depending on the site and the technology used to build it, they are made by one kind of professional or another. The craft of building a website comprises many different disciplines, and professionals usually specialize in just one part of the process. Although I do many different things, my specialization is mainly programming dynamic websites using Ruby and Rails.

In this article I want to

@afcapel
afcapel / 0-readme.md
Last active December 23, 2015 17:38 — forked from burke/0-readme.md

ruby-1.9.3-p327 cumulative performance patch for rbenv

This installs a patched ruby 1.9.3-p327 with various performance improvements and a backported COW-friendly GC, all courtesy of funny-falcon.

Requirements

You will also need a C Compiler. If you're on Linux, you probably already have one or know how to install one. On OS X, you should install XCode, and brew install autoconf using homebrew.

alberto@Albertos-iMac ~/dev/loadtime $ rails -v
Rails 4.0.2
alberto@Albertos-iMac ~/dev/loadtime $ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.5.0]
alberto@Albertos-iMac ~/dev/loadtime $ time rails runner "puts 'hello'"
hello
rails runner "puts 'hello'" 1.16s user 0.16s system 99% cpu 1.330 total