Binary Tree November 16th, 2007

Binary tree is important topic in computer science, especially in data structures field. The important characteristics are fast insertion, retrieving sorted data rapidly. Binary tree can be described like this. Every node has two leaves nodes. One or both of these two leaves nodes maybe be empty or using computer science terminology, null. Some people like to call these two leaves nodes as children nodes. Every binary tree has one root node. So there is no ambiguity for choosing starting point. After that you will have two choices every time you want to continue your journey traveling down the tree, except when the child leaf is empty or null.

We can use binary tree as efficient data structures for sorted data. When sorted data is implemented with binary tree data structures, we can rapidly store sorted data and rapidly retrieve sorted data. If you learn C/C++ and Pascal, most likely you will encounter lesson sorting data using binary tree. In C/C++ the node is implemented with struct with three fields. The first will be holder for value/data. The second and third will be pointer to this struct type data. In Ruby you can use Struct or Class as node. You don’t have pointer in Ruby but you use object reference as replacement. Most likely you will not use binary tree as data structures for sorted data in Ruby. But there are some cases that make binary tree suitable for data structures in Ruby.

Read the rest of this entry

Getting Wet With REST And Rspec (BDD) November 6th, 2007

REST and BDD are hot topics in software development. So I want to get wet with these topics. So I learned and surveyed from the internet. Okay, I think I get it. To make it practical, I try to apply these topics to Rails. The newest version of Rails (1.2.5) has support for REST. As for BBD, you have to install Rspec Rails gem.

I decided to make a simple clone of my theatre website. I develop front end only. There is no admin interfacte. I just want to learn about REST and BDD not to develop functional website. But maybe later I will add the admin interface.

Read the rest of this entry

Simple Ruby Constraint Satisfaction Problem Library August 13th, 2007

Constraint Satisfaction Problem (CSP) is a interesting topic. This was my thesis topic. CSP is something like this. Let’s say you have three variables, a, b, and c. Variable a has domain from 1 to 10 (integer). That means you can only give one of these values (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) to variable a. Variable b has domain from 5 to 15 (integer). Variable c has domain from 9 to 12 (integer). Domain is something like valid values for its variable. Then you have constraint. Let’s say we have two constraint. The first is a > c. The second is b is same as c. So we need to find the solution which need to satisfy these two constraints. Solution is described as process to give values to these three variables in one time. Giving a 2, b 7, and c 11 value is one of solution. But this is not a valid solution because it does not satisfy the first constraint. The a value (2) is lower than c value (11). The valid solution is giving a 10, b, 9, and c 9.

Read the rest of this entry

Rafaq Is Released August 13th, 2007

I know about Rails from the last year. I have bought the book (Agile Web Development With Rails first edition). Did the exercises. I also have read many articles about Rails. But I did not have a single complete Rails project. Back at the time when I was doing my thesis, I must write a program. So I used Rails. But the project was doomed because of the factory for the thesis had a very unclear requirements. The project was never finish. But my Rails skill was upgraded however.

But I still didn’t have a single complete Rails project. So in June or July, I started to write Rafaq so I can have a single complete Rails project. Yesterday the Rafaq is finished with its website. I host the project in RubyForge.

Read the rest of this entry

Selenium Takes Rails Testing to Another Level June 27th, 2007

Rails has already make testing web easier. Rails project directory has specific test directory, named surprisingly ‘test’. You want to make unit testing for model? You write it in test/unit directory. Testing for controller? test/functional directory. It has fixtures support. It means you have specific database purpose for testing purpose. Your development and production database will not be touched. It has mocks support to simulate external resources. What could you expect? Testing Rails project is a nice experience.

But Selenium takes Rails testing experience to another level. With Selenium you have user testing beside model testing, controller testing. It means you have dummy user for your Rails testing purpose. What I mean with dummy user is you have user to test your Rails web project just like you test Rails web project with browser your self. This testing type is called black box testing (external view of testing). You input data to your application and see the output. Does the output is as you expected?

Read the rest of this entry

Porting C Code to Ruby June 22nd, 2007

I try to port fsplib C library to pure ruby library. What is FSP anyway? Quoting from the official site: “FSP stands for File Service Protocol. It is a very lightweight UDP based protocol for transferring files. FSP has many benefits over FTP, mainly for running anonymous archives. FSP protocol is valuable in all kinds of environments because it is one of the only TCP/IP protocols that is not aggressive about bandwidth, while still being sufficiently fault tolerant.” It is not popular option for file transferring protocol.

So why do you want to port it anyway? Because it is a good exercise for my ruby skill. I could use it as portfolio too. Another reason is I can use this as opportunity to contribute to opensource.

I decided to port it as pure ruby library rather than binding to C api because this library does need the performance but need more portability. This is second day porting the library. Here’s what I have learned so far.

Read the rest of this entry

Haml Beautify Rails Code June 21st, 2007

Try Haml today if you use Rails. At first time, I feel reluctant. Isn’t rhtml is enough? But trying something with a little time is not a bad thing. So I jump to the boat. Here’s what I feel. It feels good.

Haml is alternative for rhtml. It is shorter. But what makes me using haml is it beatify Rails view. Usually using rhtml makes my html code (result of Rails view) cluttered.

Read the rest of this entry

Ruby On Vim June 6th, 2007

Vim is my religion. Vim is true programmers editor. It can be extended. It has many features. Using vim make you looks like a true hacker. If vim is a weapon, it could be used for short distance fighting (like knife or sword). But you can use it to shoot somebody (as pistol). Don’t underestimate it. Snipper can use it to shoot the president of a country in distance of 400m. You can destroy the buildings full of enemy soldiers with vim (think bazooka). With hard works of a reasonable amount of specific people, you can use vim to destroy a planet (okay, I am drunk now). But you get the idea. You can find many scripts to extend vim. But someday you need to extend vim your self.

Ruby is my favorite programming language. Ruby is fun. Ruby makes programmers happy. So why not combine them? Extend vim with ruby. I will guide you to write simple vim plugin using ruby. But you need to be familiar with vim and ruby (just be familiar is enough, no need to be a ninja).

You need vim compiled with ruby support. If you get vim from default place (from Linux distro maybe), most likely you have ruby support in vim. But I think you need to install vim-ruby package or something like that to enable ruby support for default vim. If you want to compile vim from source, make sure you have ruby development header. Don’t forget to use —enable-rubyinterp option in configure step. You can check if your vim has ruby support or not with this vim command.

:echo has("ruby")
If you get 1, you are all set. If you get 0, you have to set up ruby support first for your vim. Read the rest of this entry

Ruby Eval To Remove Duplications November 19th, 2006

I have been using rails extensively for about one and half months. Rails is based on ruby. So I spend time to learn ruby. I design the database with mistake. I have 8 tables that share similar attributes. Instead to make 1 table associates with 8 tables or 1 big table only (inheritance model), I made 8 different tables. The effect of this decision is I have many similar methods in my controller. For example this is one of the duplications:

class ThermoDetectorController < ApplicationController
  ....
  def new
    @thermo_detector = ThermoDetectorOrderLetter.new
    @goal = 'create'
  end
  ....
end
class QuartzHeaterController < ApplicationController
  ....
  def new
    @quartz_heater = QuartzHeaterOrderLetter.new
    @goal = 'create'
  end
  ....
end
Ruby eval to the rescue. I have to explain what is eval. It is a method from Kernel module. I evaluates the ruby expression(s) in string.
class ApplicationController < ActionController::Base
  def first_preparation(class_param)
    @class_controller = class_param
    @controller_varname = Inflector.underscore(@class_controller.to_s.gsub(/OrderLetter/, ''))
  end
  ....
  def new
    eval "@" + @controller_varname + " = @class_controller.new" 
    @goal = 'create'
  end
  ....
end
And in our controllers:
class ThermoDetectorController < ApplicationController
  ....
  def initialize
    first_preparation(ThermoDetectorOrderLetter)
  end
  ....
end
class QuartzHeaterController < ApplicationController
  ....
  def initialize
    first_preparation(QuartzHeaterOrderLetter)
  end
  ....
end

You can see I can remove new method from both controllers. I told you before I have 8 tables; that means I have 8 controllers. The new method is not the only duplication. So ruby eval is my hero. With ruby eval, I can remove duplications from models too.