Sat Aug 09 22:00:00 UTC 2008

Using Ruby to get information about a DBF file

Posted in Ruby at 10:00 PM by mohits

In a previous article, I had written about getting started with the DBF gem to convert a DBF file into a CSV file. In this article, I show how to get basic information about a DBF file by using the gem.


1
2
3
4
5
6
7
8
9
10
11
12
13
require 'rubygems'
require 'dbf'

table = DBF::Table.new(ARGV[0])

puts "Information about #{ARGV[0]}\n\n"
puts "Schema in ActiveRecord Format:"
puts table.schema
puts "\nNumber of colums  - #{table.columns.size}"
table.columns.each_with_index {|col, index|
    puts "#{index},#{col.name},#{col.type},#{col.length},#{col.decimal}"
}
puts "\nNumber of records - #{table.record_count}"

Sat Aug 09 21:05:00 UTC 2008

Working with DBF files and Ruby - a simple dbf2csv example

Posted in Ruby at 09:05 PM by mohits

Just a very short note on using Ruby to process DBF files. A number of applications still use DBF files as a simple database format (I’ve since moved on to SQLite3 – more about that some other time) – most importantly, it’s used as part of the Shapefile format.

The DBF Gem

The gem to use is called DBF and it can be got from http://dbf.rubyforge.org/dbf/ – just install it using gem install


command>gem install dbf
Bulk updating Gem source index for: http://gems.rubyforge.org
Install required dependency hoe? [Yn]  Y
Install required dependency rubyforge? [Yn]  Y
Successfully installed dbf-1.0.6
Successfully installed hoe-1.7.0
Successfully installed rubyforge-1.0.0
Installing ri documentation for dbf-1.0.6...
Installing ri documentation for hoe-1.7.0...
Installing ri documentation for rubyforge-1.0.0...
Installing RDoc documentation for dbf-1.0.6...
Installing RDoc documentation for hoe-1.7.0...
Installing RDoc documentation for rubyforge-1.0.0...

Simple Usage

As a simple example, I convert a DBF file to a comma-separated list. In this example, we check the type of field to see if it is non-numeric. Non-numeric fields are wrapped in quotes so that we don’t have to worry about commas in the text. If you are sure that none of the fields have commas, you could just proceed to output them without quotes – or you could use gsub! to replace any commas. Either way, this is just an example!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require 'rubygems'
require 'dbf'

#Load in the DBF table
table = DBF::Table.new(ARGV[0])

#wraps a field in quotes
def qt(field)
   "\"#{field}\""
end

count = 0

# Convert to CSV and output to stdout
table.records.each do |record|  #for all records
  csvr = []
  table.columns.each {|col|  #for each column in the record
    #Wrap non-numeric fields in quotes
    field = (col.type=='N')? record.attributes[col.name] : qt(record.attributes[col.name])
    csvr << field 
  }
  count +=1
  puts csvr.join(',')
end

puts "[#{db}]\t- processed #{count} records."

When using DBF, you can usually access the attribute by doing something like record.x (if x is the name of the attribute that you want) – this works fine except in cases where the name of the attribute is something like type which will give you interesting results :) That’s why for a generic example, we’ve used record.attributes[col.name] which should work in all cases.

References

Thu Jan 03 01:00:00 UTC 2008

Codegear Turbo C++ and Ruby

Posted in Codegear Builder | Turbo C++, Ruby at 01:00 AM by mohits

Embedding Ruby into a Turbo C++ (Borland C++ Builder) Application

I’m currently looking at bringing together two of my favourite pieces of desktop software – Ruby and Codegear Turbo C++. I’m still sorting my thoughts out, but here are the main links that will help in getting started. I will write up more details about this once I’ve figured it all out.

The ongoing thread in Ruby-talk that is discussing this issue

Link: http://www.ruby-forum.com/topic/137241

Specific to Borland Builder

Old threads that seem to have a few good pointers and gotchas. These are: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/73326 and very importantly: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/73397

About generally embedding Ruby

A thread that covers the idea and also links to the PickAxe online for this: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/71319

Ruby Embedded into C++

A great tutorial that goes into a lot of detail about the whole process. Link: http://metaeditor.sourceforge.net/embed/

SWIG

You could also use SWIG to connect up between a C/C++ program and Ruby. Link: http://www.swig.org/

As soon as I have the whole procedure worked out, I shall put up a post about the full procedure for integrating between the two of them.

Sat Oct 27 12:22:00 UTC 2007

Online books about Ruby/ Rails

Posted in Ruby, Ruby on Rails at 12:22 PM by mohits

Here’s a small list of online/ free books about Ruby/ Rails. This is just to keep the references in one place. Hope this helps someone else – it certainly helps me!

The PickAxe Edition 1 (Programming Ruby)

http://www.rubycentral.com/pickaxe/

Why’s Poignant Guide about Ruby

http://poignantguide.net/ruby/

Mr. Neighborly’s Humble Little Ruby Book

http://www.humblelittlerubybook.com/

A Little Ruby, A Lot of Objects

http://www.visibleworkings.com/little-ruby/

Quick References on the Language

http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html

http://www.math.hokudai.ac.jp/~gotoken/ruby/ruby-uguide/uguide08.html

Mon Mar 26 14:17:00 UTC 2007

Sending Email with Ruby

Posted in Ruby at 02:17 PM by mohits

I know that there are a number of guides online that provide the basic source for sending email using Ruby but I found that the basic sample code was lacking in one major respect – it did not set the date on the outgoing email. That meant that there was no specific “sent time” for the email and its treatment would depend on the email client. For example, Thunderbird would mark it with the date/ time at which it was downloaded.

The solution to this problem is really quite simple – you just need to format and print the date into the message as shown below.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'net/smtp'

#Senders and Recipients
from_name = 'My Name'
from_mail = 'me@mydomain.com'
to_name = 'My Friend'
to_mail = 'them@theirdomain.com'

#Servers and Authentication
smtp_host   = 'mail.mydomain.com'
smtp_port   = 25
smtp_domain = 'mydomain.com'
smtp_user   = 'user@mydomain.com'
smtp_pwd    = 'secure_password'

#The subject and the message
t = Time.now
subj = 'Sending Email with Ruby'
msg_body = "Check out the instructions on how to send mail using Ruby.\n"

#The date/time should look something like: Thu, 03 Jan 2006 12:33:22 -0700
msg_date = t.strftime("%a, %d %b %Y %H:%M:%S +0800")

#Compose the message for the email
msg = <<END_OF_MESSAGE
Date: #{msg_date}
From: #{from_name} <#{from_mail}>
To: #{to_name} <#{to_mail}>
Subject: #{subj}
  
#{msg_body}
END_OF_MESSAGE

Net::SMTP.start(smtp_host, smtp_port, smtp_domain, smtp_user, smtp_pwd, :plain) do |smtp|
  smtp.send_message msg, smtp_user, to_mail
end

And that’s all there is to it!

TODO: Need to make the time printing more generic – right now, it’s kinda hard-coded to my time zone.