By Atif Khan (
March 27, 2007 at 4:50 pm)
· Filed under Programming, Ruby, Software
Have you ever been in a situation where you wanted to write a simple application for internal use that should be portable and completely self contained (including the database)? I am pretty sure you have. I had to do this recently for one of the simple reporting applications and I didn’t really want to create a dependency on external database like MySQL or PostgreSQL. This application needed only 4 tables and the data in these table is never going to be large enough to warrant an extensive database system. I thought about putting the data in flat files and write the code to access it. But, then I found this really neat gem called KirbyBase that does exactly the same and little more.
KirbyBase has some neat features that mirror the traditional RDBMS while still sticking to it’s simple roots:
- Pure Ruby code
- Data is kept in simple delimited files, allowing the modification in any text editor
- It’s not an in-memory DB. The data is actually stored on the disk
- Allows various data types like String, Integer, Float, Boolean, Time, Date, DateTime, Memo, Blob, and YAML
- Allows creating indexes on fields
- Query results can be sorted
- Allows to define one-many links between tables similar to joins in RDBMS
- The columns can be defined with a formula, so the value will be calculated during insertion
- Automatic lookup fields can be defined to lookup the related values from another table
To give a simple example on how all this works, let’s consider a scenario of Department and Employees. The simple requirements can be laid out as each employee has personal information and belongs to a department. Also, each department has a name and identifier and can contain multiple employees. Following is how the code going to look using KirbyBase. Please keep in mind that I have tried to write a reusable DAO, hence the code may look a little verbose. You can very well short circuit the whole thing and write very concise code.
The output after execution will look like this:
Employee in IT department
Doe
John
Employee in IT department
John
Doe
This should also generate 2 files in your current directory, department.tbl and employee.tbl. Let’s look at content of these files now.
department.tbl
000001|000000|Department|recno:Integer|dept_name:String:Key->true:Index->1|employee:ResultSet:Link_many->recno=employee.dept_id
1|IT|kb_nil
employee.tbl
000002|000000|Employee|recno:Integer|dept_id:Integer:Key->true:Index->1|name:String:Key->true:Index->1
1|1|John
2|1|Doe
That’s all there to it. It is very powerful and easy to work with library.
Permalink
By Atif Khan (
November 9, 2006 at 1:44 pm)
· Filed under Programming, Ruby, Software
It is very common requirement for any reporting application to be able to draw charts based on some historical and statistical data. The languages with big user community like Java have various libraries available for creating charts. But when it comes to Ruby, there are not as many or as sophisticated choices. I was looking for a library in Ruby to do this sometime back and I found Gruff. It’s a very simple and efficient library.
To install Gruff, all you need to do is:
gem install gruff
You also need to have the RMagick installed that needs the ImageMagick to be installed. You can install ImageMagick using the package manager of your Linux distribution or download it directly from ImageMagick website and install. The RMagick installation can be done by either using gem or using the native OS package manager. On my Mandriva Linux box, the gem installation kept on failing during compilation. So, I just installed it using urpmi and it worked.
I wrote a small module in Ruby to draw the line charts. This module can be included in the scripts where you need to draw the charts. This module is called charts.rb. Here is the source code for it:
The arguments for drawChart function are as follows:
- Title of the chart
- A Hash of the data (the Caption for data => Array of data)
- A Hash of labels for the X axis
- Path of the final generated image
- The scale of the image from the original chart size of 700px*700px
- An array of hex colors if you do not want to use Gruff’s default color scheme
Now, here is how you can use the chart module and the drawChart function:
That’s pretty much all the code. In the end you should have a chart generated like following:

Permalink
By Atif Khan (
October 31, 2006 at 4:50 pm)
· Filed under Programming, Ruby, Software
Ever wondered how to set the path to some Ruby scripts you have written, so you can use them using “require”? There is a very basic solution for this problem. You need to define a system variable called RUBYLIB and point it to the directory containing your Ruby scripts. Then in your script where you want to use the library or script you created, you can simply do a “require” with the name of the script.
e.g. Supposed you created a script chart.rb in /home/foo/ruby/mylib direcory. Now, you want to use this in the app.rb located in /home/foo/ruby/scripts. Here is how you can make it work in a Linux bash environment. You could very well do this in other environments as well with minimal modifications.
In your ~/.bash_profile
export RUBYLIB=$RUBYLIB:/home/foo/ruby/mylib
and then in the app.rb script:
require 'chart'
That’s it.
Permalink