Skip to content

Instantly share code, notes, and snippets.

View vjt's full-sized avatar
💭
looking at the stars

Marcello Barnaba vjt

💭
looking at the stars
View GitHub Profile
@vjt
vjt / chronomodel-fix-db-issue-23.rb
Last active August 29, 2015 13:57
Database fix for wrongly generated ChronoModel constraints (issue #23).
# Fixes wrong database structure created by ChronoModel's issue #23
#
# https://github.com/ifad/chronomodel/issues/23
#
# Download this file in your app root:
#
# curl -O https://gist.githubusercontent.com/vjt/9861225/raw/dd2466cd948afbdb6013d0a788745f4be076ce7a/chronomodel-fix-db-issue-23.rb
#
# Run it:
#

Welcome in many languages (in JSON format)

Today I wanted to implement a multi-language greeter for an application, and I ended up on http://www.omniglot.com/language/phrases/welcome.htm

I extracted all the welcomes from the page using jQuery, serialized them as JSON and processed them using Ruby:

welcomes = x.inject({}) {|h, (lang, val)| val.gsub!(/(.?)/, ''); vals = val.split(%r{\s+/\s+|\n}); vals.map! {|val| val.strip!; val.gsub!(/^\w+ -\s+|- \w+$|^lit.|>.+$/, ''); val.strip!; val}; vals.flatten!; vals.reject!(&:blank?);; h.update(lang => vals) }

@vjt
vjt / keybase.md
Created December 15, 2014 09:43
keybase authz

Keybase proof

I hereby claim:

  • I am vjt on github.
  • I am vjt (https://keybase.io/vjt) on keybase.
  • I have a public key whose fingerprint is 723C 7CA3 3C19 2ACE 6E20 9CC1 9956 EB3C 8D27 87EF

To claim this, I am signing this object:

# Originally posted on http://pastie.org/pastes/36861 (31 January 2007)
Boolean/Mathematical Expressions Parser, built with the *excellent* Dhaka language
generation library, available on http://dhaka.rubyforge.org/.
Have a look at the extensive test suite for insights on how to use this software,
especially the protected methods in `test/boolean_expression_parser_test.rb`.
The software is licensed under the same terms as Ruby.
class Base
class_inheritable_attribute(:root) { |root| Pathname.new(root).realpath }
end
class A < Base
root '.'
end
>> Base.root
=> nil
# Originally posted on http://pastie.org/25291 (1 December 2006)
# Transmigration between two different database configurations.
# Schema must be already in place. e.g: rake db:schema:load
# Licensed in the Public domain (or the DWTFYW License, at your
# option).
# -vjt@openssl.it
source:
Originally posted on http://pastie.org/pastes/26489 (8 December 2006)
COPY FROM STDIN / LOAD DATA INFILE method for Rails PostgreSQL and MySQL Adapters.
The mysql adapter is completely untested. It is self-explanatory, isn't it? :)
Public domain, or DWTFWYW License, at your option.
-vjt@openssl.it
@vjt
vjt / README.txt
Created January 28, 2009 10:23
A simple class decorator.
A simple class decorator. Useful when implementing some sorts of the evil twin pattern.
It works by creating a blank class slate (with Class.new), removing on it EVERY
method, except `__id__`, `__send__` and `initialize`. It then defines the `class`
method to return the original class, and implements both an `initialize` and a
`method_missing`.
The `initialize` sends the `new` method to the original class, therefore creating
a new instance of the original class and saves it in the @_object instance variable.
#
# Implements the ArrayWithFinders class, that adds
# finders with names similar to ActiveRecord's ones:
#
# * find_by_XYZ -> calls the `XYZ` method on the array items,
# returing the first one on which the method
# didn't return nil/false
# * find_all_by_XYZ -> calls the `XYZ` method on the array items,
# returing all the ones on whose the method
# didn't return nil/false
# Originally posted on http://pastie.org/131174 (21 December 2007)
# Alphanum implementation -- http://www.davekoelle.com/alphanum.html
# (C) 2007 Marcello Barnaba <vjt@openssl.it>
#
# Released under the terms of the Ruby License:
# http://www.ruby-lang.org/en/LICENSE.txt
#
class String
# The algorithm breaks strings into chunks, where a chunk contains either all alphabetic characters,
# or all numeric characters. These chunks are then compared against each other. If both chunks contain