Skip to content

Instantly share code, notes, and snippets.

View ciscou's full-sized avatar

Francis Pérez Padilla ciscou

View GitHub Profile
@ciscou
ciscou / fibonacci.rb
Created September 30, 2011 19:25
Efficient yet beautiful ruby fibonacci implementation
class Fixnum
@@fibo_cache = Hash.new { |h, k| h[k] = k < 2 ? k : h[k-2] + h[k-1] }
def fibo
@@fibo_cache[self]
end
end
puts (0..500).map { |n| "#{n.to_s.rjust(3, ' ')} => #{n.fibo}" }
@ciscou
ciscou / QR Creator.pdf
Created November 2, 2011 20:26
Extracts a section of a page of a PDF into a JPG image
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ciscou
ciscou / rubik.rb
Created November 12, 2011 21:42
calculate the order of a Rubik's cube sequence
class Rubik
attr_reader :edges, :corners
def initialize
@edges = {
:position => (1..12).to_a,
:orientation => [0] * 12
}
@corners = {
:position => (1..8).to_a,
class A
def self.foo ; "foo" ; end
def self.bar ; "bar" ; end
end
class B
def self.foo ; "another foo" ; end
def self.method_missing(method_id, *args, &block)
if A.respond_to?(method_id)
A.send(method_id, *args, &block)
import java.io.File;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.validation.*;
/*
* Usage: download xsd's and sitemap
* $ wget http://www.google.com/schemas/sitemap-news/0.9/sitemap-news.xsd
* $ wget http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
@ciscou
ciscou / commit-msg
Last active December 30, 2015 05:49
commit-msg hook to ensure we're adding the Pivotal Tracker story id
#!/usr/bin/env ruby
# This is a git hook that checks commit messages for validity
# This file must live in your project's .git/hooks/ folder
# and have execution permission
message_file = ARGV[0]
message = File.read(message_file)
def valid_commit_message?(message)
class DateRange
def initialize(from, to, step)
@from, @to, @step = from, to, step
end
def each
current = @from
while current < @to do
yield(current)
current += @step
module ActiveRecord
class Base
def self.scope(name, callable)
define_singleton_method name do
options = callable.respond_to?(:call) ? callable.call : callable
puts "Fetching records with options #{options.inspect}"
end
end
end
end
@ciscou
ciscou / kw_args.rb
Last active August 29, 2015 14:04
ruby 2.0 keyword arguments
2.0.0-p451 :016 > def omg(foo: 42, bar: "hello world!")
2.0.0-p451 :017?> puts [foo, bar].inspect
2.0.0-p451 :018?> end
=> nil
2.0.0-p451 :019 > omg
[42, "hello world!"]
=> nil
2.0.0-p451 :020 > omg(1, "one")
@ciscou
ciscou / show.html.erb
Last active August 29, 2015 14:05
yes no i18n helper
is 1 == 1? <%= yesno 1 == 1 %>
is 1 == 2? <%= yesno 1 == 2 %>