среда, июня 13, 2007

Enumerable#sequenced_group_by

module Enumerable
# like general <tt>group_by</tt>, but keeps order of original collection
def sequenced_group_by
groups, hash = [], {}
each do |element|
key = yield(element)
groups << [key, hash[key]=[]] unless hash.has_key?(key)
hash[key] << element
end
groups
end
end


Example

  def test_sequenced_group_by
input = ["a2", "c1", "b1", "f1", "a1"]
result = [["a", ["a2", "a1"]], ["c", ["c1"]], ["b", ["b1"]], ["f", ["f1"]]]
assert_not_equal result, input.group_by{|x| x.at(0)}
assert_equal result, input.sequenced_group_by{|x| x.at(0)}
end

Ярлыки: , ,

понедельник, марта 19, 2007

Ruby and PostgreSql

I have problems with postgres-pr gem

This Win32 gem works http://ruby.scripting.ca/postgres/

Ярлыки: , ,