Friday, May 22, 2009

How to set up and install Rails on Ubuntu

Googling for "install rails ubuntu" shows the RailsOnUbuntu link at the RubyOnRails wiki: http://wiki.rubyonrails.org/rails/pages/RailsOnUbuntu

This page is currently blank, and in the past had outdated info. I found this page, which is much more useful:
https://help.ubuntu.com/community/RubyOnRails

Thursday, May 21, 2009

Practical Wisdom

I just watched a TED talk, given by Professor Barry Schwartz.

In his talk, Prof. Schwartz defines wisdom and it's place in society and encourages us to think about how we can encourage, rather than hamper, the development of wisdom.

Wisdom is the ability to see beyond the bureaucracy and rules of the situation and to simply do the right thing. Prof. Schwartz gives us an example of a situation practical wisdom makes a big difference and most of us don't realize it. Consider the list of requirements in a hospital janitor's job description:

They're the things you would expect: mop the floors, sweep them, empty the trash, restock the cabinets.


There is nothing unusual or unexpected on the list. Note that not one item on the list mentions any interaction with another human being. Anybody can do the job, right? Schwartz explains that there is more to being a hospital janitor than the bulleted list of job requirements:


And yet, when some psychologists interviewed hospital janitors to get a sense of what they thought their jobs were like, they encountered Mike, who told them about how he stopped mopping the floor because Mr. Jones was out of his bed getting a little exercise, trying to build up his strength, walking slowly up and down the hall. And Charlene told them about how she ignored her supervisor's admonition and didn't vacuum the visitor's lounge because there were some family members who were there all day, every day who, at this moment, happened to be taking a nap. And then there was Luke, who washed the floor in a comatose young man's room twice because the man's father, who had been keeping a vigil for six months, didn't see Luke do it the first time, and his father was angry. And behavior like this from janitors, from technicians, from nurses and, if we're lucky now and then, from doctors, doesn't just make people feel a little better, it actually improves the quality of patient care and enables hospitals to run well.


By taking into account the contingencies surrounding the patients' and family members' when performing their duties, these janitors were exhibiting practical wisdom. They had the "moral skills" to figure out what the right thing to do is and not just blindly follow procedure.

Wise people know how to improvise. He likens wise people to jazz musicians, "using the notes on the page, but dancing around them, inventing combinations that are appropriate for the situation and the people at hand."

Another point that he makes during the talk is that wise people are not born wise. They develop wisdom through years of experience with the people that they serve and can only do so when given the freedom to improvise, make mistakes and learn from them. He follows with an example in which bureaucracy stifles wisdom by forcing people to follow procedure. A father accidentally buys his son an alcoholic beverage and his son is sent to protective custody for two weeks. During the whole debacle, the people involved acknowledged that what they were doing were absurd, but that they had no choice but to follow procedure.

Finally he argues that the two tools that we reach for when things go wrong, more/better rules and more/better incentives, are ineffective at encouraging the right behavior.

Is there hope for us? In order to foster practical wisdom, we must encourage it by emphasizing the human element in the work that we do, serve as moral role models for others and instill the right values in those that we teach.

Check out the talk. It'll be a very enlightening 20 minutes.

Running the Rails console in production mode

Whenever I needed to examine or modify records in a Rails production database, I always invoked the mysql directly. I found out today that you can run the Rails console in production mode. This means that you can create model instances and call methods off of them directly. Just call the script with the desired environment as an argument:

~/railsapp]$ ./script/console production

Rolling back database changes after exiting

If you start the console with the '--sandbox' option, any database changes you make will be rolled back when you exit. This allows you to try some funky things on your data.

~/railsapp]$ ./script/console --sandbox

I found these tips, and more at this blog post (kind of old, but still very useful).

Doing single-legged pidgeon pose


The pidgeon pose provides the following benefits:
  • Stretches the thighs, groins and psoas, abdomen, chest and shoulders, and neck
  • Stimulates the abdominal organs
  • Opens the shoulders and chest

Go to Yoga Journal - One-Legged King Pigeon Pose for more info. I thought I'd try this at home.


Also, thinking about buying this yoga block. SPRI Foam Yoga Block (Purple)


Monday, March 2, 2009

Configuration file changes when moving to postgresql 8.3

Found this blog entry, which helped me when updating a postgresql.conf file to run under PostgreSQL 8.3. Summary of the changes:

stats_start_collector: This is always on so it is not necessary to configure in PostgreSQL 8.3
stats_command_string: This is now called track_activities.
stats_block_level: This is now combined with stats_row_level and renamed to track_counts
stats_row_level: This is now combined with stats_block_level and renamed to track_counts
stats_reset_on_server_start: This is no longer necessary and is not a configuration option anymore

So now the only additions you will need to make to collect all of the stats you need will be track_counts and track_activities.

Example Configuration Addition:
track_activities = on
track_counts = on

To test that you are collecting the stats run the following command:
select * from pg_stat_all_tables;

Friday, February 13, 2009

Printing exceptions in Ruby

Steve wrote a post about the simplest way to print an exception's message and backtrace in ruby.

The snippet he provides is the following:

# catch most exceptions (anything that derives from StandardError)
begin
...
rescue
puts $!, $@
end

# catch all exceptions (anything that derives from Exception)
begin
...
rescue Exception
puts $!, $@
end
end


The $! variable contains the last exception and $@ contains the last exception's backtrace as a string.

Some people (including me, before reading his post) do the following:

begin
...
rescue Exception => ex
puts ex.message
puts ex.backtrace.join("\n")
end


I'm glad I stumbled upon Steve's post. Learning about the little idioms of a language helps you to write more succinct and clear code.

Wednesday, February 4, 2009

git: prune to remove old remote tracking branches

I was trying to delete "somebranch" from a git repository today but got this error message, which baffled me for a bit:


$ git push origin :somebranch
error: unable to push to unqualified destination: somebranch
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'git@my_server:my_repo.git'


Doing a 'git branch -a' showed a "origin/somebranch" remote branch. Why can't I delete it? Then I realized that the branch might have been deleted on the remote repository and I haven't updated my remote tracking branches yet. Doing a "git pull" won't remove remote tracking branches for branches that have been deleted. To do that for a remote named "origin", you'll need to use this command:

$ git remote prune origin

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.