Sunday, March 15, 2009

Tree Traversal Ordering - Whats the order again?

I have always had trouble remembering what exactly are the orders of inorder, preorder and postorder traversals. Well today I think iv had the defining insight :). the "IN","PRE" and "POST" is based on where the root node is.


  • In "INorder" the root node is "IN" the middle of left and right nodes

  • In "PREorder" the root node is "PRE" i.e. before the left and right nodes

  • In "POSTorder" the root node is "POST" i.e. after the left and right nodes



This was that proverbial duhh moment :)

Sunday, March 08, 2009

How to test out ActiveRecord in irb

I like how interpreted languages provide this interpreter for you to try out stuff. As I am learning ruby on rails I frequently need to try out ActiveRecord's dark ways of doing things and I would like to be able to try it out in irb so here is what I do
Launch irb
irb --simple-prompt

and then execute the following code assuming you are in the rails app directory



require 'rubygems'

require 'active_record'

require 'yaml'

Dir.glob(File.join(File.dirname(__FILE__), './app/models/*.rb')).each {|f| require f }

dbconfig =
YAML::load(File.open('./config/database.yml'))

currenv =
ENV["RAILS_ENV"]

if not currenv

currenv =
"development"

end

currdbconfig = dbconfig[currenv]

currdbconfig[
"database"] = currdbconfig["database"]

#p currdbconfig

ActiveRecord::Base.establish_connection(currdbconfig)




After thins you can try out your model methods in the prompt.

This works with MySQL and should work with sqllite3 without any problems