Skip to content

Instantly share code, notes, and snippets.

@dcluna
dcluna / metaprog_csv.rb
Created March 20, 2012 17:18
Metaprog CSV
require 'csv' # ruby 1.9
class FromCSV
def initialize(file)
col_names = nil
CSV.open(file, "r") { |csv_file| col_names = csv_file.shift } # reads first line of the CSV file
@file = file
class <<self
self
end.class_eval do
@dcluna
dcluna / gist:2147164
Created March 21, 2012 14:10 — forked from fogus/gist:2124742
giftube – Generates an animated gif from a YouTube url.
#!/usr/bin/env ruby
# giftube – Generates an animated gif from a YouTube url.
#
# Usage:
#
# giftube [youtube url] [minute:second] [duration]
#
# ex.
#
@dcluna
dcluna / date_filterable.rb
Created March 22, 2012 17:29
Metaprog: dynamically add methods to a class
module DateFilterable
private
def filterable_by(field)
class_filter = %Q{def filter_by_date(min,max=nil)
if max.nil?
where("#{field} > :min_date", { :min_date => min } )
else
where("#{field} >= :min_date, #{field} < :max_date", { :min_date => min, :max_date => max } )
end
end}
@dcluna
dcluna / lisp-ruby.rb
Created April 5, 2012 02:06
Lisp in Ruby
class Risp
@@envs = []
def initialize
define_environment({})
end
def eval(arg)
if arg.is_a? Symbol # accessor
@env[arg]
elsif !arg.is_a? Array # atom - is not a list
@dcluna
dcluna / Gemfile
Created June 19, 2012 20:01 — forked from BrianTheCoder/Gemfile
an example of full text search in postgres using datamapper
source "http://gems.github.com"
source "http://gemcutter.org"
bundle_path "gems"
gem "dm-core"
gem "dm-migrations"
gem "dm-sweatshop"
gem "data_objects"
gem "do_postgres"
@dcluna
dcluna / regexparser.py
Created July 18, 2012 04:07
Regex parser in python
"Recursive-descent parser for Regexes, based on: http://matt.might.net/articles/parsing-regex-with-recursive-descent/ \
Test project for Emacs + Python"
class Parser():
"Recursive descent regex parser"
def __init__(self, regex):
"Initializes the parser with the regex string"
self.input = regex
self.current = 0
@dcluna
dcluna / gist:3306227
Created August 9, 2012 17:32
gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.11
- RUBY VERSION: 1.9.3 (2012-02-16 patchlevel 125) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/local/rvm/gems/ruby-1.9.3-p125
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-1.9.3-p125/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/rvm/gems/ruby-1.9.3-p125/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
@dcluna
dcluna / gist:3306243
Created August 9, 2012 17:34
bundle config
---
BUNDLE_BUILD__PG: --with-pg-lib=/usr/pgsql-9.1/lib/ --with-pg-config=/usr/pgsql-9.1/bin/pg_config
BUNDLE_BUILD__DO_POSTGRES: --with-pgsql-server-include=/usr/pgsql-9.1/include/server/
--with-pgsql-client-dir=/usr/pgsql-9.1/
@dcluna
dcluna / gist:3306558
Created August 9, 2012 17:58
project flow
cd receiver
export GIT_SSL_NO_VERIFY=true
bundle config build.do_postgres --with-pgsql-server-include=/usr/pgsql-9.1/include/server/ --with-pgsql-client-dir=/usr/pgsql-9.1/
bundle config build.pg --with-pg-lib=/usr/pgsql-9.1/lib/ --with-pg-config=/usr/pgsql-9.1/bin/pg_config
bundle install
cd tests
ruby basic_tests.rb
# at this point, it shows:
# /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in # `require': cannot load such file -- dm-core (LoadError)
# from /usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
@dcluna
dcluna / gist:3343716
Created August 13, 2012 20:00
Frames + Windows key setup
(define-prefix-command 'frame-map)
(define-prefix-command 'window-map)
(global-set-key (kbd "<f1>") 'frame-map)
(global-set-key (kbd "<f2>") 'window-map)
(define-key 'frame-map (kbd "RET") 'make-frame-command)
(define-key 'window-map (kbd "RET") 'make-window-command)
(define-key 'frame-map (kbd "b") 'ido-switch-buffer-other-frame) ;; todo: check if ido is active
(define-key 'window-map (kbd "b") 'ido-switch-buffer-other-window)
(define-key 'frame-map (kbd "f") 'ido-find-file-other-frame)
(define-key 'window-map (kbd "f") 'ido-find-file-other-window)