Tuesday, February 3, 2009

pv (Pipe Viewer) - a unix utility for monitoring the progress of data through a pipe

Just learned about Pipe Viewer from this blog post:
Pipe viewer is a terminal-based tool for monitoring the progress of data through a pipeline. It can be inserted into any normal pipeline between two processes to give a visual indication of how quickly data is passing through, how long it has taken, how near to completion it is, and an estimate of how long it will be until completion.

Here is the example that Peteris provides. For long-running commands, it gives you a progress indicator so you won't be left wondering, "Should I go get lunch, or wait for the gzip to be finished?"

$ pv access.log | gzip > access.log.gz
611MB 0:00:11 [58.3MB/s] [=> ] 15% ETA 0:00:59

Sunday, February 1, 2009

If you installed any of the Ubuntu flavors (Ubuntu - default with the Gnome environment, Xubuntu - a flavor that uses the Xfce desktop environment, or Kubuntu - a flavor that uses the KDE desktop environment), you can switch between them without their respective install CD's by installing the right set of packages.

I recently ran into this because I used wubi to install Kubuntu, but found that KDE was a bit "busy" for my liking. (I have a short attention span, having more things on the desktop ended up being distracting for me.)

To install Ubuntu desktop:
sudo apt-get install ubuntu-desktop

To install Xubuntu desktop:

sudo apt-get install xubuntu-desktop

To install Kubuntu desktop:

sudo apt-get install kubuntu-desktop

If you're settled with switching from one environment to the other and would like to remove all the packages from the one that you no longer use (for example, xubuntu-desktop), do the following:

sudo apt-get remove xubuntu-desktop
sudo apt-get autoremove

Tuesday, January 13, 2009

Overloading the ActiveRecord Setter

Sometimes, you want to do some extra work when you set an attribute on your ActiveRecord model instance.

You can't just do the following, because you'll just end up recursively calling the very setter that you're defining:


class Square < ActiveRecord::Base
def side=(value)
area = value * value
side = value
end
end

...and you can't do the following because you'll only modify a copy of the attributes hash of the model instance, which will not be written through to the database when the model instance is saved. ( check out ActiveRecord::Base::attributes in active_record/base.rb):

class Square < ActiveRecord::Base
def side=(value)
self.attributes['side'] = value
self.area = value * value
end
end

After looking through the source for ActiveRecord::Base and some testing, I've found a method that's worked for me and appears to be the preferred way of doing it:


class Square < ActiveRecord::Base
def side=(value)
self.area = value * value
# This would still work because we're calling the default setter for area
write_attribute('side', value)
end
end

Alternatively, you could do "self['side'] = value" instead of write_attribute. The index assignment operatore actually does the same thing.

Thursday, November 20, 2008

Ruby load path

There are three ways to refer to the variable that stores the load paths that Ruby checks for libraries when you use 'require' or 'load.'


$:
$-I
$LOAD_PATH

Tuesday, October 21, 2008

rsync: Be mindful of the trailing slash!

Pop Quiz: What's the difference between these two commands?

#1: rsync -a --delete /some/path/a/ /some/otherpath/
#2: rsync -a --delete /some/path/a /some/otherpath/


Answer: the first will make /some/otherpath mirror the contents of /some/path/a, while the latter will create a directory in /some/otherpath/a whose contents will mirror /some/path/a.

Be warned! If you're not careful and you use #1 when you intend to use #2, you can lose files in /some/otherpath.

Edit: Found this resource online: Rsync explanations and recommendations