Pragmatic Development Notes

Software development related stuff

Still on CVS? Too Bad!

At the and of May 2009 Savannah disks crashed. You can read more about it here. This is something no one can predict. You never know when your hardware will let you down, and there is no cure for that. Disks will be replaced, system reinstalled, backups restored and that’s it. Or is it?

Not completely if you are using CVS or SVN and there were some commits after the last backup. Emacs developers spent few days in the discussion to determine what commits they lost.

Luckily they had Git mirror of CVS repository but what surprises me is they didn’t move to Git yet. If they were using Git they would have been able to restore complete repository in a few minutes (maybe longer for large repositories). Since they already have Git mirror for Emacs code I really do not understand why do they not switch to Git completely. Having full history at each developer’s computer is a huge benefit. Not to mention that Git gives you possibilities you can only dream of in CVS and SVN.

Splitting Ruby Array

Recently I had to process arrays of data with more than thousand of members but requirement was not to process them one by one. Instead I had to split array into chunks and to process each of sub-arrays separately. I’ve found one definition of chunk method within comments on this post and I’m including it here. This method splits array in given number of sub-arrays and I needed a version that will split array in the sub-arrays that have predefined size (ok, I know I could simply calculate a number of chunks by dividing total number of elements by required number of elements, but I just wanted one method that’ll do all that for me). So I wrote chunk_max_num method. If maximum number of elements per array is zero or negative it will return original array and for all other cases it will return array which has arrays of given size as members.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'enumerator'

class Array
  def chunk_max_num(max_num)
    return slice(0...length) unless max_num > 0
    quot, mod = length.divmod(max_num)
    quot += 1 if mod > 0
    (0..quot).map {|i| i*max_num}.enum_cons(2).map {|a,b| slice(a...b)}
  end
 
  def chunk(pieces)
    q,r = length.divmod(pieces)
    (0..pieces).map {|i| i * q + [r,i].min}.enum_cons(2).map {|a,b| slice(a...b)}
  end
end