Skip to content

Instantly share code, notes, and snippets.

View aalin's full-sized avatar

Andrés Alin aalin

  • Colombia
View GitHub Profile
class CaseComparator
def initialize(object, operator)
@object = object
@operator = operator
end
def ===(other)
other.send(@operator, @object)
end
end
@aalin
aalin / pre-commit
Created November 12, 2011 14:49
pre-commit hook that checks syntax of changed files.
#!/usr/bin/env ruby
`git diff --cached --name-only --diff-filter=AM`.lines.each do |filename|
path = File.join(Dir.pwd, filename.strip)
puts "Checking syntax of #{ filename.strip }"
case filename
when /.haml$/
exit 1 unless system('haml', '--check', path)
when /.scss$/
exit 1 unless system('scss', '--check', path)
@aalin
aalin / gist:1688705
Created January 27, 2012 13:12
Any thoughts?
# This is not the best example, but imagine a Rails project with lots of complex logic and methods and stuff...
# Imagine that Base is an 500 line ActiveRecord model... :)
require 'rubygems'
require 'active_support/core_ext'
Customer = Struct.new(:id, :first_name, :last_name, :company_name)
## Implementation directly in the class
# Pros:
@aalin
aalin / gist:1816302
Created February 13, 2012 11:56
headache when installing capybara-webkit
# changed line 19 of rubygems/ext/builder.rb from:
mf = File.read('Makefile')
# into
mf = File.read('Makefile', :encoding => 'iso-8859-15').encode('utf-8')
# Problem was that qmake generated a Makefile that included a latin 1 "å" in the date because of swedish locales...
@aalin
aalin / locale_formatter.rb
Created March 22, 2012 17:04
Rails locale formatter
#!/usr/bin/env ruby
# encoding: utf-8
require 'yaml'
require 'stringio'
class LocaleFormatter
ESCAPE_KEYS = %w(true false yes no on off)
ESCAPE_VALUE_RE = /[^[:alnum:]_\-\/\.\?]/
MULTILINE_INDENT_LEVEL = 2
# encoding: utf-8
class LocaleDiffer
RED = 31
GREEN = 32
def initialize
load_locales
end
@aalin
aalin / schema_dumper_sequences.rb
Created August 23, 2012 12:29
Monkeypatching Rails SchemaDumper to support sequences.
class ActiveRecord::SchemaDumper
def dump(stream)
header(stream)
tables(stream)
sequences(stream)
sequence_defaults(stream)
trailer(stream)
stream
end
@aalin
aalin / t_scope.rb
Last active December 11, 2015 13:18 — forked from henrik/t_scope.rb
module I18n
# Usage (Haml in examples):
#
# - t_scope(:"public.sign_up_or_log_in") do |s|
# = simple_format(s.t(:text,
# sign_up: link_to(s.t(:sign_up), signup_path),
# log_in: link_to(s.t(:log_in), login_path)))
#
# is equivalent to
#
@aalin
aalin / gist:5129639
Last active December 14, 2015 18:29
OpenGL screenshots with ChunkyPNG
# This takes about 1.6 seconds for 800x600
def short_screenshot!(width, height)
pixels = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE)
image = ChunkyPNG::Image.from_rgb_stream(width, height, StringIO.new(pixels))
image.flip_horizontally!
image.save('screenshot.png', :fast_rgb)
end
# This takes about 0.8 seconds for 800x600
def long_screenshot!(width, height)
@aalin
aalin / catmull_rom_splines.rb
Created June 6, 2013 10:55
Catmull-Rom splines implemented in Ruby.
# Based on https://github.com/Sojo-Studios/catmull-rom/blob/master/test.js
# This implementation assumes circular splines.
class CatmullRomSplines
def initialize(points)
@key_points = points
end
def generate(detail)
points = []