Thursday, December 30, 2010

Install Mail, a Ruby Gem

Installation of mail gem is simple.
It supports both 1.8.x and 1.9.1.

$ sudo gem install mail
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.9
Successfully installed i18n-0.5.0
Successfully installed mail-2.2.13
5 gems installed
Installing ri documentation for mime-types-1.16...
Installing ri documentation for polyglot-0.3.1...
Installing ri documentation for treetop-1.4.9...
Installing ri documentation for i18n-0.5.0...
Installing ri documentation for mail-2.2.13...
Installing RDoc documentation for mime-types-1.16...
Installing RDoc documentation for polyglot-0.3.1...
Installing RDoc documentation for treetop-1.4.9...
Installing RDoc documentation for i18n-0.5.0...
Installing RDoc documentation for mail-2.2.13...
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'mail'
=> true
irb(main):003:0> exit
$ sudo gem1.9.1 install mail
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.9
Successfully installed i18n-0.5.0
Successfully installed mail-2.2.13
5 gems installed
Installing ri documentation for mime-types-1.16...
Installing ri documentation for polyglot-0.3.1...
Installing ri documentation for treetop-1.4.9...
Installing ri documentation for i18n-0.5.0...
Installing ri documentation for mail-2.2.13...
Installing RDoc documentation for mime-types-1.16...
Installing RDoc documentation for polyglot-0.3.1...
Installing RDoc documentation for treetop-1.4.9...
Installing RDoc documentation for i18n-0.5.0...
Installing RDoc documentation for mail-2.2.13...
$ irb1.9.1 
irb(main):001:0> require 'mail'
=> true
irb(main):002:0> 


Reference: https://github.com/mikel/mail

Saturday, December 18, 2010

Installing Qt-Ruby

[Update] Currently I use qtbindings instead of Qt-Ruby. The main reason I switched is that Qt-Ruby is not in active development and has not support for Ruby 1.9.x. See "Installing qtbindings on Ubuntu" (http://socrateos.blogspot.com/2011/05/installing-qtbindings.html). So the instruction below is no longer relevant for me.


1. Install Qt
Open Ubuntu Software Center from Application menu.
Find Qt Creator in Development/IDE group and install it.

2. Install Qt-Ruby
Open Synaptic Package Manager
Search for qt-ruby
Install libqt4-ruby

3. Test Qt-Ruby installation
$ irb
irb(main):001:0>require 'Qt4'
=> true
irb(main):002:0> 

4. Write simple program.
# helloworld.rb
# based on tutorial on http://www.darshancomputing.com/qt4-qtruby-tutorial/ 
# 
require 'Qt4'

app = Qt::Application.new ARGV
btn = Qt::PushButton.new 'Hello, World!'
btn.resize 100,30
btn.show

app.exec

5. Run it






6. Resouces
http://www.darshancomputing.com/qt4-qtruby-tutorial/

7. Notes.
There is no Qt-Ruby for Ruby 1.9.1 in the repositories.
Is there any, somewhere?

Wednesday, December 8, 2010

Installing Sinatra

Install sinatra.
$ sudo gem install sinatra
Successfully installed rack-1.2.1
Successfully installed tilt-1.1
Successfully installed sinatra-1.1.0
3 gems installed
Installing ri documentation for rack-1.2.1...
Installing ri documentation for tilt-1.1...
Installing ri documentation for sinatra-1.1.0...
Installing RDoc documentation for rack-1.2.1...
Installing RDoc documentation for tilt-1.1...
Installing RDoc documentation for sinatra-1.1.0...
$ sudo gem1.9.1 install sinatra
Successfully installed rack-1.2.1
Successfully installed tilt-1.1
Successfully installed sinatra-1.1.0
3 gems installed
Installing ri documentation for rack-1.2.1...
Installing ri documentation for tilt-1.1...
Installing ri documentation for sinatra-1.1.0...
Updating class cache with 2246 classes...
Installing RDoc documentation for rack-1.2.1...
Installing RDoc documentation for tilt-1.1...
Installing RDoc documentation for sinatra-1.1.0...

Test the installation.
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'sinatra'
=> true
irb(main):003:0> exit
$ irb1.9.1 
irb(main):001:0> require 'sinatra'
=> true
irb(main):002:0> exit
$ 

Sunday, December 5, 2010

Installing wxRuby (2) - failure

I tried to install wxRuby on a new laptop, and it failed.
$ irb
irb(main):003:0> require 'rubygems'
=> true
irb(main):004:0> require 'wx'
LoadError: libwx_gtk2u_stc-2.8.so.0: cannot open shared object file: No such file or directory
However, it did work after installation of wxWidgets (see my "Installing wxWidgets"). Alex Fenton says "On Linux, the gem only installs the ruby interface to the wxWidgets library, not the core wxWidgets library code itself." (http://web.archiveorange.com/archive/v/HCYBSYWLHZM4afalzm69)
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'wx'
=> true
irb(main):003:0> 

Unfortunately, in the current version of wxRuby, it only works for Ruby 1.8.7, not for 1.9.1.
~$ irb1.9.1
irb(main):001:0> require 'wx'
/var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wxruby2.so: [BUG] unknown type 0x22 (0xc given)
ruby 1.9.1p378 (2010-01-10 revision 26273) [i486-linux]

-- control frame ----------
c:0028 p:-39694812 s:0097 b:0097 l:000096 d:000096 TOP   
c:0027 p:---- s:0095 b:0095 l:000094 d:000094 CFUNC  :require
c:0026 p:0011 s:0091 b:0091 l:000090 d:000090 TOP    /var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wx.rb:12
c:0025 p:---- s:0088 b:0088 l:000087 d:000087 FINISH
c:0024 p:---- s:0086 b:0086 l:000085 d:000085 CFUNC  :require
c:0023 p:0011 s:0082 b:0082 l:00047c d:000081 EVAL   (irb):1
c:0022 p:---- s:0080 b:0080 l:000079 d:000079 FINISH
c:0021 p:---- s:0078 b:0078 l:000077 d:000077 CFUNC  :eval
c:0020 p:0027 s:0071 b:0071 l:000070 d:000070 METHOD /usr/lib/ruby/1.9.1/irb/workspace.rb:80
c:0019 p:0031 s:0064 b:0063 l:000062 d:000062 METHOD /usr/lib/ruby/1.9.1/irb/context.rb:218
c:0018 p:0030 s:0058 b:0058 l:001cec d:000057 BLOCK  /usr/lib/ruby/1.9.1/irb.rb:157
c:0017 p:0039 s:0050 b:0050 l:000049 d:000049 METHOD /usr/lib/ruby/1.9.1/irb.rb:271
c:0016 p:0011 s:0045 b:0045 l:001cec d:000044 BLOCK  /usr/lib/ruby/1.9.1/irb.rb:154
c:0015 p:0132 s:0041 b:0041 l:000024 d:000040 BLOCK  /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:244
c:0014 p:---- s:0038 b:0038 l:000037 d:000037 FINISH
c:0013 p:---- s:0036 b:0036 l:000035 d:000035 CFUNC  :loop
c:0012 p:0009 s:0033 b:0033 l:000024 d:000032 BLOCK  /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:230
c:0011 p:---- s:0031 b:0031 l:000030 d:000030 FINISH
c:0010 p:---- s:0029 b:0029 l:000028 d:000028 CFUNC  :catch
c:0009 p:0023 s:0025 b:0025 l:000024 d:000024 METHOD /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229
c:0008 p:0042 s:0022 b:0022 l:001cec d:001cec METHOD /usr/lib/ruby/1.9.1/irb.rb:153
c:0007 p:0011 s:0019 b:0019 l:000644 d:000018 BLOCK  /usr/lib/ruby/1.9.1/irb.rb:70
c:0006 p:---- s:0017 b:0017 l:000016 d:000016 FINISH
c:0005 p:---- s:0015 b:0015 l:000014 d:000014 CFUNC  :catch
c:0004 p:0174 s:0011 b:0011 l:000644 d:000644 METHOD /usr/lib/ruby/1.9.1/irb.rb:69
c:0003 p:0039 s:0006 b:0006 l:0018c4 d:001af4 EVAL   /usr/bin/irb1.9.1:12
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:0018c4 d:0018c4 TOP   
---------------------------
-- Ruby level backtrace information-----------------------------------------
/var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wx.rb:12:in `require'
/var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wx.rb:12:in `'
(irb):1:in `require'
(irb):1:in `irb_binding'
/usr/lib/ruby/1.9.1/irb/workspace.rb:80:in `eval'
/usr/lib/ruby/1.9.1/irb/workspace.rb:80:in `evaluate'
/usr/lib/ruby/1.9.1/irb/context.rb:218:in `evaluate'
/usr/lib/ruby/1.9.1/irb.rb:157:in `block (2 levels) in eval_input'
/usr/lib/ruby/1.9.1/irb.rb:271:in `signal_status'
/usr/lib/ruby/1.9.1/irb.rb:154:in `block in eval_input'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:244:in `block (2 levels) in each_top_level_statement'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:230:in `loop'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:230:in `block in each_top_level_statement'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `catch'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `each_top_level_statement'
/usr/lib/ruby/1.9.1/irb.rb:153:in `eval_input'
/usr/lib/ruby/1.9.1/irb.rb:70:in `block in start'
/usr/lib/ruby/1.9.1/irb.rb:69:in `catch'
/usr/lib/ruby/1.9.1/irb.rb:69:in `start'
/usr/bin/irb1.9.1:12:in `
' -- C level backtrace information ------------------------------------------- 0x6c7929 /usr/lib/libruby-1.9.1.so.1.9(rb_vm_bugreport+0x69) [0x6c7929] 0x5e092f /usr/lib/libruby-1.9.1.so.1.9(+0x4692f) [0x5e092f] 0x5e09ca /usr/lib/libruby-1.9.1.so.1.9(rb_bug+0x3a) [0x5e09ca] 0x5e0f0c /usr/lib/libruby-1.9.1.so.1.9(rb_check_type+0xbc) [0x5e0f0c] 0x12a2cdc /var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wxruby2.so(Init_wxAboutDialogInfo+0x8c) [0x12a2cdc] 0x16da1fb /var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wxruby2.so(InitializeOtherModules+0xb) [0x16da1fb] 0x16db109 /var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wxruby2.so(Init_wxruby2+0x339) [0x16db109] 0x5b722a /usr/lib/libruby-1.9.1.so.1.9(dln_load+0x10a) [0x5b722a] 0x5e4ae8 /usr/lib/libruby-1.9.1.so.1.9(+0x4aae8) [0x5e4ae8] 0x6b34ef /usr/lib/libruby-1.9.1.so.1.9(rb_vm_call_cfunc+0xaf) [0x6b34ef] 0x5e5ad7 /usr/lib/libruby-1.9.1.so.1.9(rb_require_safe+0x5d7) [0x5e5ad7] 0x5e5e06 /usr/lib/libruby-1.9.1.so.1.9(rb_f_require+0x26) [0x5e5e06] 0x6b4af3 /usr/lib/libruby-1.9.1.so.1.9(+0x11aaf3) [0x6b4af3] 0x6b4e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0x6b4e84] 0x6c117c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0x6c117c] 0x6bb673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0x6bb673] 0x6beb46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0x6beb46] 0x6bef0a /usr/lib/libruby-1.9.1.so.1.9(rb_iseq_eval+0x12a) [0x6bef0a] 0x5e4c8d /usr/lib/libruby-1.9.1.so.1.9(rb_load+0x18d) [0x5e4c8d] 0x5e5b18 /usr/lib/libruby-1.9.1.so.1.9(rb_require_safe+0x618) [0x5e5b18] 0x5e5e06 /usr/lib/libruby-1.9.1.so.1.9(rb_f_require+0x26) [0x5e5e06] 0x6b4af3 /usr/lib/libruby-1.9.1.so.1.9(+0x11aaf3) [0x6b4af3] 0x6b4e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0x6b4e84] 0x6c117c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0x6c117c] 0x6bb673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0x6bb673] 0x6beb46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0x6beb46] 0x6bfb1f /usr/lib/libruby-1.9.1.so.1.9(+0x125b1f) [0x6bfb1f] 0x6c014c /usr/lib/libruby-1.9.1.so.1.9(rb_f_eval+0xdc) [0x6c014c] 0x6b4b18 /usr/lib/libruby-1.9.1.so.1.9(+0x11ab18) [0x6b4b18] 0x6b4e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0x6b4e84] 0x6c117c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0x6c117c] 0x6bb673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0x6bb673] 0x6beb46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0x6beb46] 0x6c0640 /usr/lib/libruby-1.9.1.so.1.9(+0x126640) [0x6c0640] 0x5e2379 /usr/lib/libruby-1.9.1.so.1.9(rb_rescue2+0x169) [0x5e2379] 0x6b670c /usr/lib/libruby-1.9.1.so.1.9(+0x11c70c) [0x6b670c] 0x6b4afd /usr/lib/libruby-1.9.1.so.1.9(+0x11aafd) [0x6b4afd] 0x6b4e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0x6b4e84] 0x6c117c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0x6c117c] 0x6bb673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0x6bb673] 0x6beb46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0x6beb46] 0x6bf146 /usr/lib/libruby-1.9.1.so.1.9(+0x125146) [0x6bf146] 0x6bf912 /usr/lib/libruby-1.9.1.so.1.9(+0x125912) [0x6bf912] 0x6b4b18 /usr/lib/libruby-1.9.1.so.1.9(+0x11ab18) [0x6b4b18] 0x6b4e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0x6b4e84] 0x6c117c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0x6c117c] 0x6bb673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0x6bb673] 0x6beb46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0x6beb46] 0x6bf146 /usr/lib/libruby-1.9.1.so.1.9(+0x125146) [0x6bf146] 0x6bf912 /usr/lib/libruby-1.9.1.so.1.9(+0x125912) [0x6bf912] 0x6b4b18 /usr/lib/libruby-1.9.1.so.1.9(+0x11ab18) [0x6b4b18] 0x6b4e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0x6b4e84] 0x6c117c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0x6c117c] 0x6bb673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0x6bb673] 0x6beb46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0x6beb46] 0x6bedbb /usr/lib/libruby-1.9.1.so.1.9(rb_iseq_eval_main+0xab) [0x6bedbb] 0x5e2c77 /usr/lib/libruby-1.9.1.so.1.9(ruby_exec_node+0xb7) [0x5e2c77] 0x5e4276 /usr/lib/libruby-1.9.1.so.1.9(ruby_run_node+0x56) [0x5e4276] 0x80487c8 irb1.9.1(main+0x68) [0x80487c8] 0x325bd6 /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6) [0x325bd6] 0x80486c1 irb1.9.1() [0x80486c1] [NOTE] You may encounter a bug of Ruby interpreter. Bug reports are welcome. For details: http://www.ruby-lang.org/bugreport.html Aborted

Friday, December 3, 2010

Ubuntu update failed and it did not start - the filesystem does not have /sbin/init

I started normal updates through Update Manager for my Ubuntu 10.04 but it failed: just hang up in the middle of it. I had to physically shutdown the machine. And what I was afraid did happened. The Ubuntu failed to start. It did go through grub and then got an error that said.
The target filesystem doesn't have /sbin/init
No init found. Try passing init=bootarg
...

1. Retart in Recovery Mode
The first I tried to start with RECOVERY MODE that gave me an option to try to automatically fix the error. It did not fix, but it gave me an important information: which disk is having a problem. I have 4 x 1TB disks, one of which is a Window Vista. I needed to know exactly which disk to fix.
fsck from util-linux-ng 2.17.2
.....
/dev/sdd1 contains a file system with errors, check forced.
/dev/sdd1 I***s that were part of a corrupted orphan linked list found.
/dev/sdd1 UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.
(i.e., without -a or -p options)
mount all: fsck / [430] terminated with status 4
mount all: Filesystem has errors: /
So, the problem resides in /dev/sdd1.

2. Restart with Live CD and Fix with e2fsck
a. Backup my files.
After booting up with a Live CD, I mounted all drives and backed up my home directory from File System drive to another drive - just in case.
b. Unmount all drives.
Before running e2fsck, I unmounted all drives.
c. Run e2fsck.
$ sudo e2fsck -f -y -v /dev/sdd1
e2fsck 1.41.11 (14-Mar-2010)
Pass 1: Checking iノードs, blocks, and sizes
Pass 2: Checking ディレクトリ structure
Pass 3: Checking ディレクトリ connectivity
Pass 4: Checking reference counts
Pass 5: Checking グループ summary information

  309128 inodes used (0.51%)
     409 non-contiguous files (0.1%)
     329 non-contiguous directories (0.1%)
         # of inodes with ind/dind/tind blocks: 0/0/0
         Extent depth histogram: 279193/243
35484518 blocks used (14.68%)
       0 bad blocks
       1 large file

  238984 regular files
   33026 directories
      60 character device files
      26 block device files
       5 fifos
     507 links
   36963 symbolic links (29536 fast symbolic links)
      55 sockets
--------
  309626 files

3. Restart the machine.
It started without problem.


4. Rererences
http://wwww.ubuntuforums.org/showthread.php?t=1167710
ttp://wwww.ubuntuforums.org/showthread.php?t=1594621h

Thursday, November 18, 2010

Installing wxSugar

Installed wx_sugar.

$ sudo gem install wx_sugar
Successfully installed wx_sugar-0.1.22
1 gem installed
Installing ri documentation for wx_sugar-0.1.22...
Installing RDoc documentation for wx_sugar-0.1.22... 
$ sudo gem1.9.1 install wx_sugar
Successfully installed wx_sugar-0.1.22
1 gem installed
Installing ri documentation for wx_sugar-0.1.22...
Updating class cache with 2154 classes...
Installing RDoc documentation for wx_sugar-0.1.22...

Sunday, November 14, 2010

Installing wxRuby on Windows

1. Installation of wxRuby on Windows
>gem install wxruby
Successfully installed wxruby-2.0.1-x86-mingw32
1 gem installed
Installing ri documentation for wxruby-2.0.1-x86-mingw32...
Installing RDoc documentation for wxruby-2.0.1-x86-mingw32...

2. But it gets an error when running wxRuby in Ruby 1.9.2
C:\>irb
irb(main):001:0> require 'wx'
The error message window opens and says that msvcrt-ruby18.dll was not found.

3. I reinstall wxRuby using its an installer (in stead of gem install).
Download and execute:
mingw32-ruby-1.9.1-wxruby-2.0.1-setup.exe
from http://rubyforge.org/frs/?group_id=35. It automatically installs Ruby 1.9.1 with it.
C:\>ruby -v
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]
C:\>irb
irb(main):001:0> require 'wx'
=> true
irb(main):002:0> exit
So currently wxRuby is not ready for the latest Ruby (1.9.2), you have to use 1.9.1 for now. This is still better than the situation in Ubuntu as wxRuby in Ubuntu runs only in 1.8.7.

Installing Ruby1.9.2 on Windows (XP SP3)

1. Download installer from
http://files.rubyforge.vm.bytemark.co.uk/rubyinstaller/rubyinstaller-1.9.2-p0.exe

2. It installs with Mingw32.
>ruby -v
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

>gem -v
1.3.7

>irb
irb(main):001:0> 1+2
=> 3
irb(main):002:0> exit

>

Saturday, November 13, 2010

Programming wxRuby (2) - Add Menubar

1. I added a menu bar to the wxRuby program. This menu bar has only one menu (File) and one item (Exit).

#!/usr/bin/env ruby
# menu01.rb
#
require 'rubygems'
require 'wx'
 
class AppFrame < Wx::Frame

 def initialize
  super(nil)
  window_settings
  menu_settings
 end
 
 def window_settings
  set_title 'Testing Menubar'
  set_size 400,300
 end
 
 def menu_settings
  mbar = Wx::MenuBar.new
  file = Wx::Menu.new
  file.append( Wx::ID_EXIT, "E&xit\tAlt-X", "Quit" )
  mbar.append( file, "&File" )
  self.menu_bar = mbar
  evt_menu( Wx::ID_EXIT, :on_quit )
 end
 
 def on_quit
  close
 end
  
end
 
class MyApp < Wx::App
 def on_init
  @frame = AppFrame.new
  @frame.show
 end
end
 
app = MyApp.new
app.main_loop
 

2. Here is a resulted window with a menu bar.



3. References http://ruby.about.com/od/gui/a/wxrubymenu.htm

Friday, November 12, 2010

wxRuby Programming (1) - a simple window

1. My first program with wxRuby is just to display a simple window.
#!/usr/bin/env ruby
# window02.rb
#
require 'rubygems'
require 'wx'
 
class AppFrame < Wx::Frame
 
 def initialize
  super(nil)
  window_settings
 end
 
 def window_settings
  set_title 'Simple Window'
  set_size 400,300
 end
   
end
 
class MyApp < Wx::App
 def on_init
  @frame = AppFrame.new
  @frame.show
 end
end
 
app = MyApp.new
app.main_loop

2. This is the result.
3. References http://ruby.about.com/od/gui/qt/wxrubyworld.htm

Saturday, November 6, 2010

Installing wxWidgets

I installed wxWidgets lib after realizing that intallation of Code::Blocks and its wxSmith plugin did not install it. I got an error not finding while compiling in CB with a wxWidgets project.

1. Install wxwidgets lib for Ubuntu
    a. Open Synaptic
    b. Search for wx
    c. Install:
        libwxgtk2.8-dbg
        libwxgtk2-dev
        wx2.8-doc
        wx2.8-examples

2. Now try again to compile a sample program.

/***************************************************************
 * Name:      wx01App.h
 * Purpose:   Defines Application Class
 * Author:    socrateos ()
 * Created:   2010-11-06
 * Copyright: socrateos ()
 * License:
 **************************************************************/

#ifndef WX01APP_H
#define WX01APP_H

#include 

class wx01App : public wxApp
{
    public:
        virtual bool OnInit();
};

#endif // WX01APP_H

2. Here is an successful output
-------------- Build: Debug in wx01 ---------------

Compiling: wx01App.cpp
Compiling: wx01Main.cpp
Linking executable: bin/Debug/wx01
Output size is 275.88 KB
Process terminated with status 0 (0 minutes, 2 seconds)
0 errors, 0 warnings
 

Installing wxSmith plugin for Code::Block

I got an error saying wxSmith plugin is not installed when I tried to create a wxWidget project from Code::Block. So I installed wSmith plugin.

1. Open Synaptic.
2. Search for wxSmith.
3. Install codebocks-contrib, which in turn install libwxsmithlib0.

Friday, November 5, 2010

Installing wxRuby

1. Installation of wxRuby

$ sudo gem install wxruby
Successfully installed wxruby-2.0.1-x86-linux
1 gem installed
Installing ri documentation for wxruby-2.0.1-x86-linux...
Installing RDoc documentation for wxruby-2.0.1-x86-linux...
$ sudo gem1.9.1 install wxruby
Successfully installed wxruby-2.0.1-x86-linux
1 gem installed
Installing ri documentation for wxruby-2.0.1-x86-linux...
Installing RDoc documentation for wxruby-2.0.1-x86-linux...
socrateos@ubuntu-pc:~$ 

2. A Barebone App
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'wx'
=> true
irb(main):003:0> include Wx
=> Object
irb(main):004:0> class MyApp < App
irb(main):005:1> def on_init
irb(main):006:2> Frame.new(nil, -1, "Test").show()
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> MyApp.new.main_loop





3. wxRuby fails to run with Ruby 1.9.1 in Ubuntu
$ irb1.9.1 
irb(main):001:0> require 'wx'
/var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wxruby2.so: [BUG] unknown type 0x22 (0xc given)
ruby 1.9.1p378 (2010-01-10 revision 26273) [i486-linux]

-- control frame ----------
c:0028 p:-38277670 s:0097 b:0097 l:000096 d:000096 TOP   
c:0027 p:---- s:0095 b:0095 l:000094 d:000094 CFUNC  :require
c:0026 p:0011 s:0091 b:0091 l:000090 d:000090 TOP    /var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wx.rb:12
c:0025 p:---- s:0088 b:0088 l:000087 d:000087 FINISH
c:0024 p:---- s:0086 b:0086 l:000085 d:000085 CFUNC  :require
c:0023 p:0011 s:0082 b:0082 l:001204 d:000081 EVAL   (irb):1
c:0022 p:---- s:0080 b:0080 l:000079 d:000079 FINISH
c:0021 p:---- s:0078 b:0078 l:000077 d:000077 CFUNC  :eval
c:0020 p:0027 s:0071 b:0071 l:000070 d:000070 METHOD /usr/lib/ruby/1.9.1/irb/workspace.rb:80
c:0019 p:0031 s:0064 b:0063 l:000062 d:000062 METHOD /usr/lib/ruby/1.9.1/irb/context.rb:218
c:0018 p:0030 s:0058 b:0058 l:001bb4 d:000057 BLOCK  /usr/lib/ruby/1.9.1/irb.rb:157
c:0017 p:0039 s:0050 b:0050 l:000049 d:000049 METHOD /usr/lib/ruby/1.9.1/irb.rb:271
c:0016 p:0011 s:0045 b:0045 l:001bb4 d:000044 BLOCK  /usr/lib/ruby/1.9.1/irb.rb:154
c:0015 p:0132 s:0041 b:0041 l:000024 d:000040 BLOCK  /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:244
c:0014 p:---- s:0038 b:0038 l:000037 d:000037 FINISH
c:0013 p:---- s:0036 b:0036 l:000035 d:000035 CFUNC  :loop
c:0012 p:0009 s:0033 b:0033 l:000024 d:000032 BLOCK  /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:230
c:0011 p:---- s:0031 b:0031 l:000030 d:000030 FINISH
c:0010 p:---- s:0029 b:0029 l:000028 d:000028 CFUNC  :catch
c:0009 p:0023 s:0025 b:0025 l:000024 d:000024 METHOD /usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229
c:0008 p:0042 s:0022 b:0022 l:001bb4 d:001bb4 METHOD /usr/lib/ruby/1.9.1/irb.rb:153
c:0007 p:0011 s:0019 b:0019 l:00059c d:000018 BLOCK  /usr/lib/ruby/1.9.1/irb.rb:70
c:0006 p:---- s:0017 b:0017 l:000016 d:000016 FINISH
c:0005 p:---- s:0015 b:0015 l:000014 d:000014 CFUNC  :catch
c:0004 p:0174 s:0011 b:0011 l:00059c d:00059c METHOD /usr/lib/ruby/1.9.1/irb.rb:69
c:0003 p:0039 s:0006 b:0006 l:000a94 d:000784 EVAL   /usr/bin/irb1.9.1:12
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:000a94 d:000a94 TOP   
---------------------------
-- Ruby level backtrace information-----------------------------------------
/var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wx.rb:12:in `require'
/var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wx.rb:12:in `'
(irb):1:in `require'
(irb):1:in `irb_binding'
/usr/lib/ruby/1.9.1/irb/workspace.rb:80:in `eval'
/usr/lib/ruby/1.9.1/irb/workspace.rb:80:in `evaluate'
/usr/lib/ruby/1.9.1/irb/context.rb:218:in `evaluate'
/usr/lib/ruby/1.9.1/irb.rb:157:in `block (2 levels) in eval_input'
/usr/lib/ruby/1.9.1/irb.rb:271:in `signal_status'
/usr/lib/ruby/1.9.1/irb.rb:154:in `block in eval_input'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:244:in `block (2 levels) in each_top_level_statement'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:230:in `loop'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:230:in `block in each_top_level_statement'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `catch'
/usr/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `each_top_level_statement'
/usr/lib/ruby/1.9.1/irb.rb:153:in `eval_input'
/usr/lib/ruby/1.9.1/irb.rb:70:in `block in start'
/usr/lib/ruby/1.9.1/irb.rb:69:in `catch'
/usr/lib/ruby/1.9.1/irb.rb:69:in `start'
/usr/bin/irb1.9.1:12:in `
' -- C level backtrace information ------------------------------------------- 0xb777c929 /usr/lib/libruby-1.9.1.so.1.9(rb_vm_bugreport+0x69) [0xb777c929] 0xb769592f /usr/lib/libruby-1.9.1.so.1.9(+0x4692f) [0xb769592f] 0xb76959ca /usr/lib/libruby-1.9.1.so.1.9(rb_bug+0x3a) [0xb76959ca] 0xb7695f0c /usr/lib/libruby-1.9.1.so.1.9(rb_check_type+0xbc) [0xb7695f0c] 0xb6d41cdc /var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wxruby2.so(Init_wxAboutDialogInfo+0x8c) [0xb6d41cdc] 0xb71791fb /var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wxruby2.so(InitializeOtherModules+0xb) [0xb71791fb] 0xb717a109 /var/lib/gems/1.9.1/gems/wxruby-2.0.1-x86-linux/lib/wxruby2.so(Init_wxruby2+0x339) [0xb717a109] 0xb766c22a /usr/lib/libruby-1.9.1.so.1.9(dln_load+0x10a) [0xb766c22a] 0xb7699ae8 /usr/lib/libruby-1.9.1.so.1.9(+0x4aae8) [0xb7699ae8] 0xb77684ef /usr/lib/libruby-1.9.1.so.1.9(rb_vm_call_cfunc+0xaf) [0xb77684ef] 0xb769aad7 /usr/lib/libruby-1.9.1.so.1.9(rb_require_safe+0x5d7) [0xb769aad7] 0xb769ae06 /usr/lib/libruby-1.9.1.so.1.9(rb_f_require+0x26) [0xb769ae06] 0xb7769af3 /usr/lib/libruby-1.9.1.so.1.9(+0x11aaf3) [0xb7769af3] 0xb7769e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0xb7769e84] 0xb777617c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0xb777617c] 0xb7770673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0xb7770673] 0xb7773b46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0xb7773b46] 0xb7773f0a /usr/lib/libruby-1.9.1.so.1.9(rb_iseq_eval+0x12a) [0xb7773f0a] 0xb7699c8d /usr/lib/libruby-1.9.1.so.1.9(rb_load+0x18d) [0xb7699c8d] 0xb769ab18 /usr/lib/libruby-1.9.1.so.1.9(rb_require_safe+0x618) [0xb769ab18] 0xb769ae06 /usr/lib/libruby-1.9.1.so.1.9(rb_f_require+0x26) [0xb769ae06] 0xb7769af3 /usr/lib/libruby-1.9.1.so.1.9(+0x11aaf3) [0xb7769af3] 0xb7769e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0xb7769e84] 0xb777617c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0xb777617c] 0xb7770673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0xb7770673] 0xb7773b46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0xb7773b46] 0xb7774b1f /usr/lib/libruby-1.9.1.so.1.9(+0x125b1f) [0xb7774b1f] 0xb777514c /usr/lib/libruby-1.9.1.so.1.9(rb_f_eval+0xdc) [0xb777514c] 0xb7769b18 /usr/lib/libruby-1.9.1.so.1.9(+0x11ab18) [0xb7769b18] 0xb7769e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0xb7769e84] 0xb777617c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0xb777617c] 0xb7770673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0xb7770673] 0xb7773b46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0xb7773b46] 0xb7775640 /usr/lib/libruby-1.9.1.so.1.9(+0x126640) [0xb7775640] 0xb7697379 /usr/lib/libruby-1.9.1.so.1.9(rb_rescue2+0x169) [0xb7697379] 0xb776b70c /usr/lib/libruby-1.9.1.so.1.9(+0x11c70c) [0xb776b70c] 0xb7769afd /usr/lib/libruby-1.9.1.so.1.9(+0x11aafd) [0xb7769afd] 0xb7769e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0xb7769e84] 0xb777617c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0xb777617c] 0xb7770673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0xb7770673] 0xb7773b46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0xb7773b46] 0xb7774146 /usr/lib/libruby-1.9.1.so.1.9(+0x125146) [0xb7774146] 0xb7774912 /usr/lib/libruby-1.9.1.so.1.9(+0x125912) [0xb7774912] 0xb7769b18 /usr/lib/libruby-1.9.1.so.1.9(+0x11ab18) [0xb7769b18] 0xb7769e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0xb7769e84] 0xb777617c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0xb777617c] 0xb7770673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0xb7770673] 0xb7773b46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0xb7773b46] 0xb7774146 /usr/lib/libruby-1.9.1.so.1.9(+0x125146) [0xb7774146] 0xb7774912 /usr/lib/libruby-1.9.1.so.1.9(+0x125912) [0xb7774912] 0xb7769b18 /usr/lib/libruby-1.9.1.so.1.9(+0x11ab18) [0xb7769b18] 0xb7769e84 /usr/lib/libruby-1.9.1.so.1.9(+0x11ae84) [0xb7769e84] 0xb777617c /usr/lib/libruby-1.9.1.so.1.9(+0x12717c) [0xb777617c] 0xb7770673 /usr/lib/libruby-1.9.1.so.1.9(+0x121673) [0xb7770673] 0xb7773b46 /usr/lib/libruby-1.9.1.so.1.9(+0x124b46) [0xb7773b46] 0xb7773dbb /usr/lib/libruby-1.9.1.so.1.9(rb_iseq_eval_main+0xab) [0xb7773dbb] 0xb7697c77 /usr/lib/libruby-1.9.1.so.1.9(ruby_exec_node+0xb7) [0xb7697c77] 0xb7699276 /usr/lib/libruby-1.9.1.so.1.9(ruby_run_node+0x56) [0xb7699276] 0x80487c8 irb1.9.1(main+0x68) [0x80487c8] 0xb748cbd6 /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6) [0xb748cbd6] 0x80486c1 irb1.9.1() [0x80486c1] [NOTE] You may encounter a bug of Ruby interpreter. Bug reports are welcome. For details: http://www.ruby-lang.org/bugreport.html Aborted

Tuesday, November 2, 2010

Installing devhelp

Installed devhelp, GTK library reference tool.

1. Open Synaptic.
2. Find and install devhelp.

Sunday, October 31, 2010

Reinstalling Google Chrome in Ubuntu

After my latest Ubuntu updates, google chrome stopped working. It stopped launching.
So I reinstalled it from Google website, and it now works.
The reinstalling was very simple.

1. Find out if my Ubuntu is 32bit or 64bit.
(I know my CPU - AMD Phenom 9650 - is 64 bit capable. System menu:System:System Monitor )

$ uname -m
$ i686
"i686" means it is a 32 bit kernel.
(See http://stackoverflow.com/questions/246007/how-to-determine-whether-a-given-linux-is-32-bit-or-64-bit)

2. Go to the Google Chrome download site for Linux.
http://www.google.com/chrome/eula.html?platform=linux_ubuntu_i386

3. Select "32 bit .deb(Debian/Ubuntu)" and Click "Agree and Install".

4. I rebooted Ubuntu - just in case.

Now it works again and my bookmarks are untouched.

Saturday, October 23, 2010

GTK Programming in Ruby (7) - Adding Panes

This ruby program demonstrates a window with paned widgets.

1. Main program - panes01.rb
#!/usr/bin/ruby
#
# panes01.rb
#
# using UIManager
# requires menu_toolbar.ui, ui_module03.rb

require 'gtk2'
require 'ui_module03.rb'

class App < Gtk::Window
 
 include UIModule # 'ui_module03.rb'
 
 def initialize
  super
  window_settings('Panes', 800, 500, Gtk::Window::POS_CENTER)
  ui_initialize                   
  layout_settings 
  event_settings
  self.show_all
 end
  
 def window_settings(title, width, height, position)
  self.set_title title
  self.set_default_size width, height
  self.set_border_width 1
    self.set_window_position position
 end

 def layout_settings
  vbox = Gtk::VBox.new false, 4
  vbox.pack_start ui_create_menu, false, false, 0
  vbox.pack_start ui_create_toolbar, false, false, 0
  vbox.pack_start ui_create_panes, true, true, 0
  vbox.pack_start ui_create_statusbar, false, false, 0
  self.add vbox
  return vbox
 end
 
 def event_settings
  self.signal_connect "destroy" do
   app_quit
  end
 end   
 
 def app_quit
  Gtk.main_quit 
 end
 
end

Gtk.init
 window = App.new
Gtk.main
2. An ui file to define menubar, toolbar, and status bar.
<ui>
  <menubar name="MenuBar">
    <menu name="FileMenu" action="File">
   <menuitem name="FileNew" action="New"/>
      <menuitem name="FileOpen" action="Open"/>
      <separator/>
      <menuitem name="FileSave" action="Save"/>
      <menuitem name="FileSaveAs" action="SaveAs"/>
      <menuitem name="FileRevert" action="Revert"/>
      <separator/>
      <menuitem name="FilePageSetup" action="PageSetup"/>
      <menuitem name="FilePreview" action="Preview"/> 
      <menuitem name="FilePrint" action="Print"/> 
      <separator/>      
      <menuitem name="FileQuit" action="Quit"/>
    </menu>
    <menu name="EditMenu" action="Edit">
   <menuitem name="EditUndo" action="Undo"/>
   <menuitem name="EditRedo" action="Redo"/>
   <separator/>  
      <menuitem name="EditCut" action="Cut"/>
      <menuitem name="EditCopy" action="Copy"/>
      <menuitem name="EditPaste" action="Paste"/>
      <menuitem name="EditClear" action="Clear"/>
      <separator/>
      <menuitem name="EditSelectAll" action="SelectAll"/>
      <menuitem name="EditDeselect" action="Deselect"/>
      <separator/>
      <menuitem name="EditPreferences" action="Preferences"/>
    </menu>
    <menu name="HelpMenu" action="Help">
      <menuitem name="HelpContents" action="Contents"/>
      <menuitem name="HelpAbout" action="About"/>
    </menu>
  </menubar>
  <toolbar name="ToolBar">
   <toolitem name="FileNew" action="New"/>
      <toolitem name="FileOpen" action="Open"/>
      <toolitem name="FileSave" action="Save"/>
      <toolitem name="FileSaveAs" action="SaveAs"/>
      <toolitem name="FileRevert" action="Revert"/>
      <toolitem name="FilePreview" action="Preview"/> 
      <toolitem name="FilePrint" action="Print"/> 
      <separator/>  
   <toolitem name="EditUndo" action="Undo"/>
   <toolitem name="EditRedo" action="Redo"/>
      <toolitem name="EditCut" action="Cut"/>
      <toolitem name="EditCopy" action="Copy"/>
      <toolitem name="EditPaste" action="Paste"/>
      <toolitem name="EditClear" action="Clear"/>
      <toolitem name="EditSelectAll" action="SelectAll"/>
      <toolitem name="EditPreferences" action="Preferences"/>
   <separator/>
      <toolitem name="HelpContents" action="Contents"/>
      <toolitem name="HelpAbout" action="About"/>
      <separator/>
      <toolitem name="FileQuit" action="Quit"/>
  </toolbar>
</ui>
3. A module to define ui widgets handler.
# ui_module03.rb
#
# require 'menuar_toolbar.ui'

module UIModule

 def ui_initialize
 
  ui_create_statusbar
  
  # callback procs! 
  new       = Proc.new { set_statusbar "new" }
  open      = Proc.new { set_statusbar "open" }
  save      = Proc.new { set_statusbar "save" }
  saveas    = Proc.new { set_statusbar "saveas" }
  revert    = Proc.new { set_statusbar "revert" }
  pagesetup = Proc.new { set_statusbar "pagesetup" }
  preview   = Proc.new { set_statusbar "preview" }
  print     = Proc.new { set_statusbar "print" }
  quit    = Proc.new { app_quit }
  undo      = Proc.new { set_statusbar "undo" }
  redochg   = Proc.new { set_statusbar "redo" }
  cut       = Proc.new { set_statusbar "cut" }
  copy      = Proc.new { set_statusbar "copy" }
  paste     = Proc.new { set_statusbar "paste" }
  clear     = Proc.new { set_statusbar "clear" }
  selectall = Proc.new { set_statusbar "selectall" }
  deselect  = Proc.new { set_statusbar "deselect" }
  preferences = Proc.new { set_statusbar "preferences" }
  help      = Proc.new { set_statusbar "help" }
  about     = Proc.new { set_statusbar "about" }

  # menu items: name, stock_id, label, accelerator, tool-tip, callback
  entries = [
   ["File", nil, "_File", nil, nil, nil ],
    ["New",      Gtk::Stock::NEW,  nil, nil, "Create New",   new],
    ["Open",      Gtk::Stock::OPEN,  nil, nil, "Open a file",   open],
    ["Save",      Gtk::Stock::SAVE,  nil, nil, "Save the doc.", save],
    ["SaveAs",    Gtk::Stock::SAVE_AS,  nil, nil, "Save the doc. as", saveas],
    ["Revert",    Gtk::Stock::REVERT_TO_SAVED,  nil, nil, "Revert to saved", revert],
    ["PageSetup", nil,"Page Setup", nil, "Page setup", pagesetup],
    ["Preview",    Gtk::Stock::PRINT_PREVIEW,  nil, nil, "Print Preview", preview],
    ["Print",      Gtk::Stock::PRINT,  nil, nil, "Save the doc.", save],
    ["Quit",      Gtk::Stock::QUIT,  nil, nil, "Quit the app.", quit],
   ["Edit", nil, "_Edit", nil, nil, nil],
    ["Undo",       Gtk::Stock::UNDO,    nil, nil, "Undo Change",  undo],
    ["Redo",       Gtk::Stock::REDO,    nil, nil, "Redo Change",  redochg],
    ["Cut",       Gtk::Stock::CUT,    nil, nil, "Cut the sel.",  cut],
    ["Copy",      Gtk::Stock::COPY,   nil, nil, "Copy the sel.", copy],
    ["Paste",     Gtk::Stock::PASTE,  nil, nil, "Paste text",    paste],
    ["Clear",     Gtk::Stock::CLEAR,  nil, nil, "Clear text",    clear],
    ["SelectAll", Gtk::Stock::SELECT_ALL, nil, nil, "Sel all",  selectall],
    ["Deselect",  nil, "_Deselect", "<ctl>d", "Deselect all",   deselect],
    ["Preferences",Gtk::Stock::PREFERENCES,  nil, nil, "Preferences", preferences],
   ["Help", nil, "_Help", nil, nil, nil],
    ["Contents",  Gtk::Stock::HELP,  nil, nil, "Get help",      help],
    ["About",     Gtk::Stock::ABOUT, nil, nil, "More info ...", about]
   ]
  
  # create an action group with entries defined above
  group = Gtk::ActionGroup.new("ActionGroup")
  group.add_actions(entries)
  
  # create ui manager object and add action group
  @ui_mgr = Gtk::UIManager.new
   @ui_mgr.insert_action_group group,0
   self.add_accel_group @ui_mgr.accel_group
   
 end
 
 # create menubar using ui manager
 def ui_create_menu   
  return ui_get_widget("menu_toolbar.ui", "/MenuBar") 
 end
 
 # create toolbar using ui manager
 def ui_create_toolbar
   return ui_get_widget("menu_toolbar.ui", "/ToolBar") 
 end

 # utility function to create a widget using ui manager
 def ui_get_widget(uifile, name)
  @ui_mgr.add_ui uifile
   widget = @ui_mgr.get_widget name   
   return widget
 end 

 # create status bar
 def ui_create_statusbar
  @statusbar = Gtk::Statusbar.new
  return @statusbar
 end

 # set statusbar message
 def set_statusbar(message)
  context = @statusbar.get_context_id('StatusMessage')
  @statusbar.pop(context)
  @statusbar.push(context,message)
 end
 
 # create two panes, placing a notebook on each
 def ui_create_panes
  panes = Gtk::HPaned.new
  notebook1 = Gtk::Notebook.new
  notebook2 = Gtk::Notebook.new
  notebook1.append_page(Gtk::Frame.new, Gtk::Label.new('A'))
  notebook1.append_page(Gtk::Frame.new, Gtk::Label.new('B'))
  notebook2.append_page(Gtk::Frame.new, Gtk::Label.new('1'))
  notebook2.append_page(Gtk::Frame.new, Gtk::Label.new('2'))
  notebook2.append_page(Gtk::Frame.new, Gtk::Label.new('3'))
  notebook2.append_page(Gtk::Frame.new, Gtk::Label.new('4'))
  panes.pack1 notebook1, true, true
  panes.pack2 notebook2, true, true
  return panes
 end
 
end
4. Run it.
$ ruby panes01.rb

5. Results.


Saturday, October 16, 2010

Changing the width of my blogger page

I changed the width of my blogger page.

1. Go to Design mode.
2. Click the Edit HTML tab.
3. Search for "template-skin".
4. Find the following section.
<b:template-skin>
<b:variable default='930px' name='content.width' type='length' value='1000px'/>
<b:variable default='0' name='main.column.left.width' type='length' value='360px'/>
<b:variable default='360px' name='main.column.right.width' type='length' value='0px'/>
5. Change the content.width to '1150px' and left.width to '200px'.
These changes minimize the width of left column and maximize content width without invoking a horizontal scrollbar in my 19 inch monitor.
<b:template-skin>
<b:variable default='930px' name='content.width' type='length' value='1150px'/>
<b:variable default='0' name='main.column.left.width' type='length' value='200px'/>
<b:variable default='360px' name='main.column.right.width' type='length' value='0px'/>

Testing ruby-dicom

I tested ruby-dicom to read and display a sample dicom image.

1. Here is a ruby program that read a dicom file and display its image using RMagick.
# test-dicom.rb
# 
# read a sample dicom file and displays it using RMagick lib
#
# based on sample: 
# http://github.com/cuthbert/ruby-dicom
# The sample dicom file was downloaded from: 
# http://www.barre.nom.fr/medical/samples/

require 'rubygems'
require 'dicom'
require 'RMagick'
include Magick

obj = DICOM::DObject.new("samples/MR-MONO2-16-knee.dcm")
obj.information
obj.print
image = obj.get_image_magick.normalize
image.display

2. Here is the image displayed by RMagick.



3. Here is the terminal output.
$ ruby test-dicom.rb

System Properties:
-------------------------------------------
Ruby DICOM version:   0.8
Byte Order (CPU):     Little Endian

DICOM Object Properties:
-------------------------------------------
File:                 samples/MR-MONO2-16-knee.dcm
Modality:             MR Image Storage
Value Representation: Implicit
Byte Order (File):    Little Endian
Pixel Data:           Yes
Image Size:           256*256
Number of frames:     1
Photometry:           MONOCHROME2
Compression:          No
Bits per Pixel:       16
-------------------------------------------
 1 0002,0000 File Meta Information Group Length UL      4 180
 2 0002,0001 File Meta Information Version      OB      2 
 3 0002,0002 Media Storage SOP Class UID        UI     26 1.2.840.10008.5.1.4.1.1.4
 4 0002,0003 Media Storage SOP Instance UID     UI     44 1.2.840.113619.2.1.2.139348932.2.9.602503126
 5 0002,0010 Transfer Syntax UID                UI     18 1.2.840.10008.1.2
 6 0002,0012 Implementation Class UID           UI     18 1.2.840.113619.6.5
 7 0002,0013 Implementation Version Name        SH      6 1_2_5
 8 0002,0016 Source Application Entity Title    AE      6 sdc21
 9 0008,0000 Group Length                       UL      4 414
10 0008,0008 Image Type                         CS     16 ORIGINAL\PRIMARY
11 0008,0016 SOP Class UID                      UI     26 1.2.840.10008.5.1.4.1.1.4
12 0008,0018 SOP Instance UID                   UI     44 1.2.840.113619.2.1.2.139348932.2.9.602503126
13 0008,0020 Study Date                         DA     10 1989.02.03
14 0008,0021 Series Date                        DA     10 1989.02.03
15 0008,0023 Content Date                       DA     10 1989.02.03
16 0008,0030 Study Time                         TM      8 09:26:18
17 0008,0031 Series Time                        TM      8 09:58:19
18 0008,0033 Content Time                       TM      8 09:58:46
19 0008,0060 Modality                           CS      2 MR
20 0008,0070 Manufacturer                       LO     18 GE MEDICAL SYSTEMS
21 0008,0080 Institution Name                   LO     28 THOMAS JEFF UNIVHOSPITAL MRI
22 0008,0090 Referring Physician's Name         PN     10 Anonymized
23 0008,1010 Station Name                       SH      8 FOR.IC0
24 0008,1030 Study Description                  LO      4 KNEE
25 0008,103E Series Description                 LO      6 SAG 3D
26 0008,1060 Name of Physician(s) Reading Study PN     10 Anonymized
27 0008,1070 Operators' Name                    PN     10 Anonymized
28 0008,1090 Manufacturer's Model Name          LO     14 GENESIS_SIGNA
29 0010,0000 Group Length                       UL      4 18
30 0010,0010 Patient's Name                     PN     10 Anonymized
31 0018,0000 Group Length                       UL      4 368
32 0018,0010 Contrast/Bolus Agent               LO      4 NONE
33 0018,0020 Scanning Sequence                  CS      2 GR
34 0018,0021 Sequence Variant                   CS      2 GR
35 0018,0022 Scan Options                       CS     10 GRAPH_GEMS
36 0018,0023 MR Acquisition Type                CS      2 3D
37 0018,0025 Angio Flag                         CS      2 N
38 0018,0050 Slice Thickness                    DS      8 3.000000
39 0018,0080 Repetition Time                    DS     10 33.000000
40 0018,0081 Echo Time                          DS     10 15.000000
41 0018,0083 Number of Averages                 DS      8 1.000000
42 0018,0084 Imaging Frequency                  DS     12 0.0000000000
43 0018,0085 Imaged Nucleus                     SH      2 H1
44 0018,0086 Echo Number(s)                     IS      2 1
45 0018,0087 Magnetic Field Strength            DS      6 15000
46 0018,0088 Spacing Between Slices             DS      8 3.000000
47 0018,0091 Echo Train Length                  IS      2 0
48 0018,0093 Percent Sampling                   DS     10 100.000000
49 0018,0094 Percent Phase Field of View        DS     10 100.000000
50 0018,1062 Nominal Interval                   IS      6 33000
51 0018,1088 Heart Rate                         IS      2 0
52 0018,1090 Cardiac Number of Images           IS      2 0
53 0018,1094 Trigger Window                     IS      2 10
54 0018,1100 Reconstruction Diameter            DS     14 160.0000000000
55 0018,1314 Flip Angle                         DS      2 30
56 0018,1315 Variable Flip Angle Flag           CS      2 N
57 0018,1316 SAR                                DS      8 0.009903
58 0018,5100 Patient Position                   CS      4 FFS
59 0020,0000 Group Length                       UL      4 304
60 0020,000D Study Instance UID                 UI     40 1.2.840.113619.2.1.2.139348932.602501178
61 0020,000E Series Instance UID                UI     42 1.2.840.113619.2.1.2.139348932.2.602503099
62 0020,0011 Series Number                      IS      2 2
63 0020,0012 Acquisition Number                 IS      2 0
64 0020,0013 Instance Number                    IS      2 9
65 0020,0032 Image Position (Patient)           DS     32 16.500000\-80.000000\ 84.726562
66 0020,0037 Image Orientation (Patient)        DS     56 0.000000\1.000000\0.000000\0.000000\0.000000\ -1.000000
67 0020,0052 Frame of Reference UID             UI     32 1.2.840.113619.2.1.2.139348932.0
68 0020,1040 Position Reference Indicator       LO      2 KN
69 0020,1041 Slice Location                     DS     14 -16.5000000000
70 0028,0000 Group Length                       UL      4 132
71 0028,0002 Samples per Pixel                  US      2 1
72 0028,0004 Photometric Interpretation         CS     12 MONOCHROME2
73 0028,0010 Rows                               US      2 256
74 0028,0011 Columns                            US      2 256
75 0028,0030 Pixel Spacing                      DS     12 0.625\0.625
76 0028,0100 Bits Allocated                     US      2 16
77 0028,0101 Bits Stored                        US      2 16
78 0028,0102 High Bit                           US      2 15
79 0028,0103 Pixel Representation               US      2 1
80 0028,1050 Window Center                      DS      4 1095
81 0028,1051 Window Width                       DS      2 84
82 7FE0,0000 Group Length                       UL      4 131080
83 7FE0,0010 Pixel Data                         OW 131072 

4. Resources.

a. Ruby-dicom official home page: http://dicom.rubyforge.org/
b. github: http://github.com/cuthbert/ruby-dicom
c. Ruby-dicom forum: http://groups.google.com/group/ruby-dicom
d. Dicom sample images: http://www.barre.nom.fr/medical/samples/

Monday, October 11, 2010

Installing aeskulap - DICOM viewer - on Ubuntu

I installed aeskulap, a DICOM viewer, on my Ubuntu.

1. Open Synaptic
2. Find and install aeskulap

Sunday, October 10, 2010

Installing RMagick on Ubuntu

I installed RMagic.

1. Open Synaptic.
2. Select and install:
rmagic # 2.21-4
librmagic-ruby # libmatic-ruby1.8
librmagic-ruby-doc

(Note: I do not see rmagic for ruby 1.9.1.)

3. Test run.
$ cd images/
$ ls
bardejov.jpg  mincol.jpg  redrock.jpg  rotunda.jpg
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'RMagick'
=> true
irb(main):003:0> include Magick
=> Object
irb(main):004:0> pic = ImageList.new 'redrock.jpg'
=> [redrock.jpg JPEG 150x112 150x112+0+0 DirectClass 8-bit 2kb]
scene=0
irb(main):005:0> pic.display  # this displays the image in a separate window
=> [redrock.jpg JPEG 150x112 150x112+0+0 DirectClass 8-bit 2kb]
scene=0
irb(main):006:0> exit
$ 

4. Here is the displayed image

5. Resources

http://www.imagemagick.org/RMagick/doc/usage.html

Saturday, October 9, 2010

Installing Ruby-DICOM

Installed dicom gem.

1. Install for ruby 1.8
$ sudo gem install dicom
Successfully installed dicom-0.8
1 gem installed
Installing ri documentation for dicom-0.8...
Installing RDoc documentation for dicom-0.8...

2. Install for ruby 1.9.1
$ sudo gem1.9.1 install dicom
Successfully installed dicom-0.8
1 gem installed
Installing ri documentation for dicom-0.8...
Updating class cache with 1787 classes...
Installing RDoc documentation for dicom-0.8...

3. Test installations.
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'dicom'
=> true
irb(main):003:0> quit
$ irb1.9.1 
irb(main):001:0> require 'dicom'
=> true
irb(main):002:0> 


4 Resouces

http://github.com/cuthbert/ruby-dicom

Sunday, October 3, 2010

GTK Programming in Ruby (6) - Adding Status Bar

1. Main
#
# statusbar01.rb
#

require 'gtk2'
require 'ui_module02.rb'

class App < Gtk::Window
 
 include UIModule # 'ui_module02.rb'
 
 def initialize
  super
  window_settings('Status Bar', 700, 500, Gtk::Window::POS_CENTER)
  ui_initialize                   
  layout_settings 
  event_settings
  self.show_all
 end
  
 def window_settings(title, width, height, position)
  self.set_title title
  self.set_default_size width, height
    self.set_window_position position
 end

 def layout_settings
  vbox = Gtk::VBox.new false, 5
  vbox.pack_start ui_create_menu, false, false, 0
  vbox.pack_start ui_create_toolbar, false, false, 0
  vbox.pack_start Gtk::Label.new(''), true, false, 0
  vbox.pack_start ui_create_statusbar, false, false, 0
  self.add vbox
  return vbox
 end
 
 def event_settings
  self.signal_connect "destroy" do
   app_quit
  end
 end   
 
 def app_quit
  Gtk.main_quit 
 end
 
end

Gtk.init
 window = App.new
Gtk.main

2. UI Module
#
# ui_module02.rb
#
module UIModule

 def ui_initialize
 
  ui_create_statusbar
  
  # callback procs! 
  new       = Proc.new { set_statusbar "new" }
  open      = Proc.new { set_statusbar "open" }
  save      = Proc.new { set_statusbar "save" }
  saveas    = Proc.new { set_statusbar "saveas" }
  revert    = Proc.new { set_statusbar "revert" }
  pagesetup = Proc.new { set_statusbar "pagesetup" }
  preview   = Proc.new { set_statusbar "preview" }
  print     = Proc.new { set_statusbar "print" }
  quit    = Proc.new { app_quit }
  undo      = Proc.new { set_statusbar "undo" }
  redochg   = Proc.new { set_statusbar "redo" }
  cut       = Proc.new { set_statusbar "cut" }
  copy      = Proc.new { set_statusbar "copy" }
  paste     = Proc.new { set_statusbar "paste" }
  clear     = Proc.new { set_statusbar "clear" }
  selectall = Proc.new { set_statusbar "selectall" }
  deselect  = Proc.new { set_statusbar "deselect" }
  preferences = Proc.new { set_statusbar "preferences" }
  help      = Proc.new { set_statusbar "help" }
  about     = Proc.new { set_statusbar "about" }

  # menu items: name, stock_id, label, accelerator, tool-tip, callback
  entries = [
   ["File", nil, "_File", nil, nil, nil ],
    ["New",      Gtk::Stock::NEW,  nil, nil, "Create New",   new],
    ["Open",      Gtk::Stock::OPEN,  nil, nil, "Open a file",   open],
    ["Save",      Gtk::Stock::SAVE,  nil, nil, "Save the doc.", save],
    ["SaveAs",    Gtk::Stock::SAVE_AS,  nil, nil, "Save the doc. as", saveas],
    ["Revert",    Gtk::Stock::REVERT_TO_SAVED,  nil, nil, "Revert to saved", revert],
    ["PageSetup", nil,"Page Setup", nil, "Page setup", pagesetup],
    ["Preview",    Gtk::Stock::PRINT_PREVIEW,  nil, nil, "Print Preview", preview],
    ["Print",      Gtk::Stock::PRINT,  nil, nil, "Save the doc.", save],
    ["Quit",      Gtk::Stock::QUIT,  nil, nil, "Quit the app.", quit],
   ["Edit", nil, "_Edit", nil, nil, nil],
    ["Undo",       Gtk::Stock::UNDO,    nil, nil, "Undo Change",  undo],
    ["Redo",       Gtk::Stock::REDO,    nil, nil, "Redo Change",  redochg],
    ["Cut",       Gtk::Stock::CUT,    nil, nil, "Cut the sel.",  cut],
    ["Copy",      Gtk::Stock::COPY,   nil, nil, "Copy the sel.", copy],
    ["Paste",     Gtk::Stock::PASTE,  nil, nil, "Paste text",    paste],
    ["Clear",     Gtk::Stock::CLEAR,  nil, nil, "Clear text",    clear],
    ["SelectAll", Gtk::Stock::SELECT_ALL, nil, nil, "Sel all",  selectall],
    ["Deselect",  nil, "_Deselect", "d", "Deselect all",   deselect],
    ["Preferences",Gtk::Stock::PREFERENCES,  nil, nil, "Preferences", preferences],
   ["Help", nil, "_Help", nil, nil, nil],
    ["Contents",  Gtk::Stock::HELP,  nil, nil, "Get help",      help],
    ["About",     Gtk::Stock::ABOUT, nil, nil, "More info ...", about]
   ]
  
  # create an action group with entries defined above
  group = Gtk::ActionGroup.new("ActionGroup")
  group.add_actions(entries)
  
  # create ui manager object and add action group
  @ui_mgr = Gtk::UIManager.new
   @ui_mgr.insert_action_group group,0
   self.add_accel_group @ui_mgr.accel_group
   
 end
 
 # create menubar using ui manager
 def ui_create_menu   
  return ui_get_widget("menu_toolbar.ui", "/MenuBar") 
 end
 
 # create toolbar using ui manager
 def ui_create_toolbar
   return ui_get_widget("menu_toolbar.ui", "/ToolBar") 
 end

 # utility function to create a widget using ui manager
 def ui_get_widget(uifile, name)
  @ui_mgr.add_ui uifile
   widget = @ui_mgr.get_widget name   
   return widget
 end 

 # create status bar
 def ui_create_statusbar
  @statusbar = Gtk::Statusbar.new
  return @statusbar
 end

 # set statusbar message
 def set_statusbar(message)
  context = @statusbar.get_context_id('StatusMessage')
  @statusbar.pop(context)
  @statusbar.push(context,message)
 end
 
end


3. ui file (menu_toolbar.ui)
<ui>
  <menubar name="MenuBar">
    <menu name="FileMenu" action="File">
   <menuitem name="FileNew" action="New"/>
      <menuitem name="FileOpen" action="Open"/>
      <separator/>
      <menuitem name="FileSave" action="Save"/>
      <menuitem name="FileSaveAs" action="SaveAs"/>
      <menuitem name="FileRevert" action="Revert"/>
      <separator/>
      <menuitem name="FilePageSetup" action="PageSetup"/>
      <menuitem name="FilePreview" action="Preview"/> 
      <menuitem name="FilePrint" action="Print"/> 
      <separator/>      
      <menuitem name="FileQuit" action="Quit"/>
    </menu>
    <menu name="EditMenu" action="Edit">
   <menuitem name="EditUndo" action="Undo"/>
   <menuitem name="EditRedo" action="Redo"/>
   <separator/>  
      <menuitem name="EditCut" action="Cut"/>
      <menuitem name="EditCopy" action="Copy"/>
      <menuitem name="EditPaste" action="Paste"/>
      <menuitem name="EditClear" action="Clear"/>
      <separator/>
      <menuitem name="EditSelectAll" action="SelectAll"/>
      <menuitem name="EditDeselect" action="Deselect"/>
      <separator/>
      <menuitem name="EditPreferences" action="Preferences"/>
    </menu>
    <menu name="HelpMenu" action="Help">
      <menuitem name="HelpContents" action="Contents"/>
      <menuitem name="HelpAbout" action="About"/>
    </menu>
  </menubar>
  <toolbar name="ToolBar">
   <toolitem name="FileNew" action="New"/>
      <toolitem name="FileOpen" action="Open"/>
      <toolitem name="FileSave" action="Save"/>
      <toolitem name="FileSaveAs" action="SaveAs"/>
      <toolitem name="FileRevert" action="Revert"/>
      <toolitem name="FilePreview" action="Preview"/> 
      <toolitem name="FilePrint" action="Print"/> 
      <separator/>  
   <toolitem name="EditUndo" action="Undo"/>
   <toolitem name="EditRedo" action="Redo"/>
      <toolitem name="EditCut" action="Cut"/>
      <toolitem name="EditCopy" action="Copy"/>
      <toolitem name="EditPaste" action="Paste"/>
      <toolitem name="EditClear" action="Clear"/>
      <toolitem name="EditSelectAll" action="SelectAll"/>
      <toolitem name="EditPreferences" action="Preferences"/>
   <separator/>
      <toolitem name="HelpContents" action="Contents"/>
      <toolitem name="HelpAbout" action="About"/>
      <separator/>
      <toolitem name="FileQuit" action="Quit"/>
  </toolbar>
</ui>


4. Run it.
$ ruby statusbar01.rb
5. Result.

Friday, September 24, 2010

GTK Programming in Ruby (5) - Adding Toolbar, using UIManager

1. Main program showing a window with a toolbar.
#
# toolbar01.rb
#
require 'gtk2'
require 'ui_module.rb'

class App < Gtk::Window
 
 include UIModule # 'ui_module.rb'
 
 def initialize
  super
  window_settings('Toolbar using UIManager', 700, 500, Gtk::Window::POS_CENTER)
  ui_initialize # 'ui_module.rb'
  vbox = layout_settings(ui_create_menu,ui_create_toolbar) # 'ui_module.rb'
  event_settings
  self.show_all
 end
  
 def window_settings(title, width, height, position)
  self.set_title title
  self.set_default_size width, height
    self.set_window_position position
 end

 def layout_settings(menubar,toolbar)
  vbox = Gtk::VBox.new false, 4
  vbox.pack_start menubar, false, false, 0
  vbox.pack_start toolbar, false, false, 0
  self.add vbox
  return vbox
 end
 
 def event_settings
  self.signal_connect "destroy" do
   app_quit
  end
 end   
 
 def app_quit
  Gtk.main_quit 
 end
 
end

Gtk.init
 window = App.new
Gtk.main


2. Define Menu and Tool bar structure in a separate file.

menu_toolbar.ui

<ui>
  <menubar name="MenuBar">
    <menu name="FileMenu" action="File">
   <menuitem name="FileNew" action="New"/>
      <menuitem name="FileOpen" action="Open"/>
      <separator/>
      <menuitem name="FileSave" action="Save"/>
      <menuitem name="FileSaveAs" action="SaveAs"/>
      <menuitem name="FileRevert" action="Revert"/>
      <separator/>
      <menuitem name="FilePageSetup" action="PageSetup"/>
      <menuitem name="FilePreview" action="Preview"/> 
      <menuitem name="FilePrint" action="Print"/> 
      <separator/>      
      <menuitem name="FileQuit" action="Quit"/>
    </menu>
    <menu name="EditMenu" action="Edit">
   <menuitem name="EditUndo" action="Undo"/>
   <menuitem name="EditRedo" action="Redo"/>
   <separator/>  
      <menuitem name="EditCut" action="Cut"/>
      <menuitem name="EditCopy" action="Copy"/>
      <menuitem name="EditPaste" action="Paste"/>
      <menuitem name="EditClear" action="Clear"/>
      <separator/>
      <menuitem name="EditSelectAll" action="SelectAll"/>
      <menuitem name="EditDeselect" action="Deselect"/>
      <separator/>
      <menuitem name="EditPreferences" action="Preferences"/>
    </menu>
    <menu name="HelpMenu" action="Help">
      <menuitem name="HelpContents" action="Contents"/>
      <menuitem name="HelpAbout" action="About"/>
    </menu>
  </menubar>
  <toolbar name="ToolBar">
   <toolitem name="FileNew" action="New"/>
      <toolitem name="FileOpen" action="Open"/>
      <toolitem name="FileSave" action="Save"/>
      <toolitem name="FileSaveAs" action="SaveAs"/>
      <toolitem name="FileRevert" action="Revert"/>
      <toolitem name="FilePreview" action="Preview"/> 
      <toolitem name="FilePrint" action="Print"/> 
      <separator/>  
   <toolitem name="EditUndo" action="Undo"/>
   <toolitem name="EditRedo" action="Redo"/>
      <toolitem name="EditCut" action="Cut"/>
      <toolitem name="EditCopy" action="Copy"/>
      <toolitem name="EditPaste" action="Paste"/>
      <toolitem name="EditClear" action="Clear"/>
      <toolitem name="EditSelectAll" action="SelectAll"/>
      <toolitem name="EditPreferences" action="Preferences"/>
   <separator/>
      <toolitem name="HelpContents" action="Contents"/>
      <toolitem name="HelpAbout" action="About"/>
      <separator/>
      <toolitem name="FileQuit" action="Quit"/>
  </toolbar>
</ui>


3. UI manager handler.

#
# ui_module.rb
#

module UIModule

 def ui_initialize
 
  # callback procs! 
  new       = Proc.new { puts "new" }
  open      = Proc.new { puts "open" }
  save      = Proc.new { puts "save" }
  saveas    = Proc.new { puts "saveas" }entries
  revert    = Proc.new { puts "revert" }
  pagesetup = Proc.new { puts "pagesetup" }
  preview   = Proc.new { puts "preview" }
  print     = Proc.new { puts "print" }
  quit    = Proc.new { app_quit }
  undo      = Proc.new { puts "undo" }
  redochg   = Proc.new { puts "redo" }
  cut       = Proc.new { puts "cut" }
  copy      = Proc.new { puts "copy" }
  paste     = Proc.new { puts "paste" }
  clear     = Proc.new { puts "clear" }
  selectall = Proc.new { puts "selectall" }
  deselect  = Proc.new { puts "deselect" }
  preferences = Proc.new { puts "preferences" }
  help      = Proc.new { puts "help" }
  about     = Proc.new { puts "about" }

  # menu items: name, stock_id, label, accelerator, tool-tip, callback
  entries = [
   ["File", nil, "_File", nil, nil, nil ],
    ["New",      Gtk::Stock::NEW,  nil, nil, "Create New",   new],
    ["Open",      Gtk::Stock::OPEN,  nil, nil, "Open a file",   open],
    ["Save",      Gtk::Stock::SAVE,  nil, nil, "Save the doc.", save],
    ["SaveAs",    Gtk::Stock::SAVE_AS,  nil, nil, "Save the doc. as", saveas],
    ["Revert",    Gtk::Stock::REVERT_TO_SAVED,  nil, nil, "Revert to saved", revert],
    ["PageSetup", nil,"Page Setup", nil, "Page setup", pagesetup],
    ["Preview",    Gtk::Stock::PRINT_PREVIEW,  nil, nil, "Print Preview", preview],
    ["Print",      Gtk::Stock::PRINT,  nil, nil, "Save the doc.", save],
    ["Quit",      Gtk::Stock::QUIT,  nil, nil, "Quit the app.", quit],
   ["Edit", nil, "_Edit", nil, nil, nil],
    ["Undo",       Gtk::Stock::UNDO,    nil, nil, "Undo Change",  undo],
    ["Redo",       Gtk::Stock::REDO,    nil, nil, "Redo Change",  redochg],
    ["Cut",       Gtk::Stock::CUT,    nil, nil, "Cut the sel.",  cut],
    ["Copy",      Gtk::Stock::COPY,   nil, nil, "Copy the sel.", copy],
    ["Paste",     Gtk::Stock::PASTE,  nil, nil, "Paste text",    paste],
    ["Clear",     Gtk::Stock::CLEAR,  nil, nil, "Clear text",    clear],
    ["SelectAll", Gtk::Stock::SELECT_ALL, nil, nil, "Sel all",  selectall],
    ["Deselect",  nil, "_Deselect", "d", "Deselect all",   deselect],
    ["Preferences",Gtk::Stock::PREFERENCES,  nil, nil, "Preferences", preferences],
   ["Help", nil, "_Help", nil, nil, nil],
    ["Contents",  Gtk::Stock::HELP,  nil, nil, "Get help",      help],
    ["About",     Gtk::Stock::ABOUT, nil, nil, "More info ...", about]
   ]
  
  # create an action group with entries defined above
  group = Gtk::ActionGroup.new("ActionGroup")
  group.add_actions(entries)
  
  # create ui manager object and add action group
  @ui_mgr = Gtk::UIManager.new
   @ui_mgr.insert_action_group group,0
   self.add_accel_group @ui_mgr.accel_group
   
 end
 
 # create menubar using ui manager
 def ui_create_menu   
  return ui_get_widget("menu_toolbar.ui", "/MenuBar") 
 end
 
 # create toolbar using ui manager
 def ui_create_toolbar
   return ui_get_widget("menu_toolbar.ui", "/ToolBar") 
 end

 # utility function to create a widget using ui manager
 def ui_get_widget(uifile, name)
  @ui_mgr.add_ui uifile
   widget = @ui_mgr.get_widget name   
   return widget
 end 
 
end

4. Run it.
$ ruby toolbar01.rb

5. Result.

Wednesday, September 15, 2010

GTK Programming in Ruby (4) - Adding Menubar, using UIManager

1. Create a ui file (xml format) to define its menu structure.
File Name: menu03.ui
<ui>
  <menubar name="MenuBar">
    <menu name="FileMenu" action="File">
   <menuitem name="FileNew" action="New"/>
      <menuitem name="FileOpen" action="Open"/>
      <separator/>
      <menuitem name="FileSave" action="Save"/>
      <menuitem name="FileSaveAs" action="SaveAs"/>
      <menuitem name="FileRevert" action="Revert"/>
      <separator/>
      <menuitem name="FilePageSetup" action="PageSetup"/>
      <menuitem name="FilePreview" action="Preview"/> 
      <menuitem name="FilePrint" action="Print"/> 
      <separator/>      
      <menuitem name="FileQuit" action="Quit"/>
    </menu>
    <menu name="EditMenu" action="Edit">
   <menuitem name="EditUndo" action="Undo"/>
   <menuitem name="EditRedo" action="Redo"/>
   <separator/>  
      <menuitem name="EditCut" action="Cut"/>
      <menuitem name="EditCopy" action="Copy"/>
      <menuitem name="EditPaste" action="Paste"/>
      <menuitem name="EditClear" action="Clear"/>
      <separator/>
      <menuitem name="EditSelectAll" action="SelectAll"/>
      <menuitem name="EditDeselect" action="Deselect"/>
      <separator/>
      <menuitem name="EditPreferences" action="Preferences"/>
    </menu>
    <menu name="HelpMenu" action="Help">
      <menuitem name="HelpContents" action="Contents"/>
      <menuitem name="HelpAbout" action="About"/>
    </menu>
  </menubar>
</ui>

2. Use it in the program to create menu.
#!/usr/bin/ruby
#
# menu03.rb
# using UIManager
# requires menu03.ui

require 'gtk2'

class App < Gtk::Window
 
 def initialize
  super
  window_settings('Menubar using UIManager', 400, 300, Gtk::Window::POS_CENTER)
  mb = menu_settings
  vbox = layout_settings(mb)
  event_settings
  self.show_all
 end
  
 def window_settings(title, width, height, position)
  self.set_title title
  self.set_default_size width, height
    self.set_window_position position
 end

 def layout_settings(menubar)
  vbox = Gtk::VBox.new false, 3
  vbox.pack_start menubar, false, false, 0
  self.add vbox
  return vbox
 end
 
 def menu_settings    
 
  # callback procs! 
  new      = Proc.new { puts "new" }
  open      = Proc.new { puts "open" }
  save      = Proc.new { puts "save" }
  saveas    = Proc.new { puts "saveas" }
  revert    = Proc.new { puts "revert" }
  pagesetup = Proc.new { puts "pagesetup" }
  preview   = Proc.new { puts "preview" }
  print     = Proc.new { puts "print" }
  quit    = Proc.new { app_quit }
  undo      = Proc.new { puts "undo" }
  redochg   = Proc.new { puts "redo" }
  cut       = Proc.new { puts "cut" }
  copy      = Proc.new { puts "copy" }
  paste     = Proc.new { puts "paste" }
  clear     = Proc.new { puts "clear" }
  selectall = Proc.new { puts "selectall" }
  deselect  = Proc.new { puts "deselect" }
  preferences = Proc.new { puts "preferences" }
  help      = Proc.new { puts "help" }
  about     = Proc.new { puts "about" }

  # menu items: name, stock_id, label, accelerator, tool-tip, callback
  entries = [
   ["File", nil, "_File", nil, nil, nil ],
    ["New",      Gtk::Stock::NEW,  nil, nil, "Create New",   new],
    ["Open",      Gtk::Stock::OPEN,  nil, nil, "Open a file",   open],
    ["Save",      Gtk::Stock::SAVE,  nil, nil, "Save the doc.", save],
    ["SaveAs",    Gtk::Stock::SAVE_AS,  nil, nil, "Save the doc. as", saveas],
    ["Revert",    Gtk::Stock::REVERT_TO_SAVED,  nil, nil, "Revert to saved", revert],
    ["PageSetup", nil,"Page Setup", nil, "Page setup", pagesetup],
    ["Preview",    Gtk::Stock::PRINT_PREVIEW,  nil, nil, "Print Preview", preview],
    ["Print",      Gtk::Stock::PRINT,  nil, nil, "Save the doc.", save],
    ["Quit",      Gtk::Stock::QUIT,  nil, nil, "Quit the app.", quit],
   ["Edit", nil, "_Edit", nil, nil, nil],
    ["Undo",       Gtk::Stock::UNDO,    nil, nil, "Undo Change",  undo],
    ["Redo",       Gtk::Stock::REDO,    nil, nil, "Redo Change",  redochg],
    ["Cut",       Gtk::Stock::CUT,    nil, nil, "Cut the sel.",  cut],
    ["Copy",      Gtk::Stock::COPY,   nil, nil, "Copy the sel.", copy],
    ["Paste",     Gtk::Stock::PASTE,  nil, nil, "Paste text",    paste],
    ["Clear",     Gtk::Stock::CLEAR,  nil, nil, "Clear text",    clear],
    ["SelectAll", Gtk::Stock::SELECT_ALL, nil, nil, "Sel all",  selectall],
    ["Deselect",  nil, "_Deselect", "d", "Deselect all",   deselect],
    ["Preferences",Gtk::Stock::PREFERENCES,  nil, nil, "Preferences", preferences],
   ["Help", nil, "_Help", nil, nil, nil],
    ["Contents",  Gtk::Stock::HELP,  nil, nil, "Get help",      help],
    ["About",     Gtk::Stock::ABOUT, nil, nil, "More info ...", about]
  ]
    
  return create_menu(entries,"menu03.ui")
  
 end
 
 def event_settings
  self.signal_connect "destroy" do
   app_quit
  end
 end   
 
 def create_menu(entries, menu_ui)
  
  group = Gtk::ActionGroup.new("MenuActionGroup")
  group.add_actions(entries)
  
  ui = Gtk::UIManager.new
   ui.insert_action_group group,0
   ui.add_ui menu_ui
   
   menubar = ui.get_widget "/MenuBar"
   
  self.add_accel_group ui.accel_group
   
   return menubar
 end
 
 def app_quit
  Gtk.main_quit 
 end
 
end

Gtk.init
 window = App.new
Gtk.main

3. Run it:
$ ruby menu03.rb 

4. Results:
a. File menu

b. Edit menu

5, Issues.
a.Again, it automatically translate (into Japanese). How do I suppress, or display in another language such as German or Chinese?
b. Is there a stock item for Page Setup?

6. Reference:
a. http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-dynmc
b. http://zetcode.com/tutorials/rubygtktutorial/menustoolbars/

Sunday, September 12, 2010

Installed Simple Backup (sbackup)

Just installed backup software on my Ubuntu PC.

1. Open Synapitc.
2. Search and Install sbackup.
3. Administration > Backup

I am using a default settings for now.

Saturday, September 11, 2010

Installing rbbr - ruby class browser

Installed rbbr.

1. Open Synaptic
2. Search and Install rbbr

Launch it.

$ rbbr


It opens a browser like this:

GTK Programming in Ruby (3) - Adding Menubar, using Stock items

#!/usr/bin/ruby
#
# menu02.rb
# using Stock Menu Items

require 'gtk2'

class App < Gtk::Window
 
 def initialize
  super
  window_settings('Window with Menu', 400, 300)
  mb = menu_settings
  vbox = layout_settings(mb)
  event_settings
  show_all
 end
 
 def window_settings(title, width, height)
  set_title title
  set_default_size width, height
    set_window_position Gtk::Window::POS_CENTER
 end

 def layout_settings(menubar)
  vbox = Gtk::VBox.new false, 3
  vbox.pack_start menubar, false, false, 0
  add vbox
  return vbox
 end
 
 def menu_settings   
 
  # create keyboard accelerator group (keyboard shot cut)
  @keyaccgrp = Gtk::AccelGroup.new
    add_accel_group @keyaccgrp
   
  # menu bar
  menubar = Gtk::MenuBar.new
      
  # menus
  filemenu = new_menu(menubar, 'File')
  editmenu = new_menu(menubar, 'Edit')
  helpmenu = new_menu(menubar, 'Help')
  
  # file menu items
  quitmenuitem = new_menu_item (filemenu, Gtk::Stock::QUIT, 'Q').signal_connect "activate" do
   app_quit
  end     

  # edit menu items
  copymenuitem = new_menu_item (editmenu, Gtk::Stock::COPY, 'C')
  cutmenuitem = new_menu_item (editmenu, Gtk::Stock::CUT, 'X')
  pastemenuitem = new_menu_item (editmenu, Gtk::Stock::PASTE, 'V')
 
  return menubar
  
 end
 
 def event_settings
  signal_connect "destroy" do
   app_quit
  end   
 end  
 
 def new_menu(menubar,title)
  menu = Gtk::Menu.new
  item = Gtk::MenuItem.new title
  item.set_submenu menu
  menubar.append item
  return menu 
 end
 
 def new_menu_item(menu, stock, char)
  item = Gtk::ImageMenuItem.new stock, @keyaccgrp
    key, mod = Gtk::Accelerator.parse char
    item.add_accelerator("activate", @keyaccgrp, key, mod, Gtk::ACCEL_VISIBLE)  
  menu.append item
  return item
 end
 
 def app_quit
  Gtk.main_quit
 end
 
end

Gtk.init
 window = App.new
Gtk.main




The result:

Issues:
1. I did not expect automatic translation (to Japanese). How do I suppress?
2. How do I add accelerator to Menu bar items such as "File"? Using underscore ("_File") does not work.

Friday, September 10, 2010

GTK Programming in Ruby (2) - Adding Menubar

# menu01.rb

require 'gtk2'

class App < Gtk::Window

 def initialize
  super
  window_settings('Window with Menu', 400, 300)
  menu_settings
  event_settings
  show_all
 end
    
 def window_settings(title, width, height)
  set_title title
  set_default_size width, height
    set_window_position Gtk::Window::POS_CENTER
 end

 def menu_settings    
 
  # menu bar
  menubar = Gtk::MenuBar.new
  
  # file menu
  filemenu = Gtk::Menu.new
  filemenuitem = Gtk::MenuItem.new "File"
  filemenuitem.set_submenu filemenu
  
  # file | quit menu item
  exitmenuitem = Gtk::MenuItem.new "Quit"
  exitmenuitem.signal_connect "activate" do
    quit
  end
  filemenu.append exitmenuitem

  # edit menu
  editmenu = Gtk::Menu.new
  editmenuitem = Gtk::MenuItem.new "Edit"
  editmenuitem.set_submenu editmenu
  
  # help menu
  helpmenu = Gtk::Menu.new
  helpmenuitem = Gtk::MenuItem.new "Help"
  helpmenuitem.set_submenu helpmenu

  # append all menu(item)s to menubar
  menubar.append filemenuitem
  menubar.append editmenuitem
  menubar.append helpmenuitem
  
  # place the menu bar in the window
  vbox = Gtk::VBox.new false, 2
  vbox.pack_start menubar, false, false, 0
  add vbox
  
 end
 
 def event_settings
  signal_connect "destroy" do 
   quit
  end    
 end   
 
 def quit
  Gtk.main_quit 
 end
 
end

Gtk.init
 window = App.new
Gtk.main

Run it:
$ ruby menu01.rb

The result is:

Monday, September 6, 2010

Testing sequel to access PostgreSQL

# seq01pg.rb

require 'rubygems'
require 'sequel'

class SequelTest

  def initialize
    
    @db =  Sequel.postgres('mydb', :host=>'localhost', :user=>'socrateos', :password=>'secret')
    
    unless(@db.table_exists?(:table1))
      @db.create_table :table1 do
        primary_key :id
        String :title
      end
    end
  
    unless(record_exists?(:table1, :title, 'test1'))
      @db[:table1].insert(:id => 1, :title => 'test1')
    end
  
    unless(record_exists?(:table1, :title, 'test2'))
      @db[:table1].insert(:id => 2, :title => 'test2')
    end
 
    unless(record_exists?(:table1, :title, 'test3'))
      @db[:table1].insert(:id => 3, :title => 'test3')
    end
  
  end

  def record_exists?(table, field, value)
    rows = @db[table].where(field => value)
    return (rows.count>0)
  end
  
  def show_all
    p "Database type:" + @db.database_type.to_s
    @db.fetch('select * from table1') do |row|
      p row
    end
  end
 
end

app = SequelTest.new
app.show_all


Run it:
$ ruby seq01pg.rb 
"Database type:postgres"
{:title=>"test1", :id=>1}
{:title=>"test2", :id=>2}
{:title=>"test3", :id=>3}

Saturday, September 4, 2010

Testing sequel to access SQLite

require 'rubygems'
require 'sequel'

class SequelTest

  def initialize
    # @db = Sequle.connect("sqlite:test2.db") # this does not create database
    @db = Sequel.sqlite("test2.db")
    unless(@db.table_exists?(:table1))
      @db.create_table :table1 do
        primary_key :id
        String :title
      end
    end
  
    unless(record_exists?(:table1, :title, 'test1'))
      @db[:table1].insert(:title => 'test1')
    end
  
    unless(record_exists?(:table1, :title, 'test2'))
      @db[:table1].insert(:title => 'test2')
    end
  
    unless(record_exists?(:table1, :title, 'test3'))
      @db[:table1].insert(:title => 'test3')
    end
  
  end

  def record_exists?(table, field, value)
    rows = @db[table].where(field => value)
    return (rows.count>0)
  end
  
  def show_all
    p "Database type:" + @db.database_type.to_s
    @db.fetch('select * from table1') do |row|
      p row
    end
  end

end

app = SequelTest.new
app.show_all

$ ruby seq01sq.rb 
"Database type:sqlite"
{:title=>"test1", :id=>1}
{:title=>"test2", :id=>2}
{:title=>"test3", :id=>3}

Wednesday, September 1, 2010

Testing pg (postgresql-ruby)

# pgsql01.rb

require 'pg'

class PgSQLTest

  def initialize
    @db = PGconn.connect( nil, 5432, nil, nil, 'mydb', nil, nil ) 
    puts "dbname: " + @db.db + ", user: "+@db.user
    @db.exec("create table test (id INTEGER PRIMARY KEY, title TEXT)") \
       unless(table_exists("test"))
    @db.exec("insert into test (id, title) values(1, 'test1')") \
       unless(record_exists("test", "test1"))
    @db.exec("insert into test (id, title) values(2, 'test2')") \
       unless(record_exists("test", "test2"))
  end
 
  def exit
    @db.close if(@db)
  end
 
  def table_exists(name)
    rows = @db.exec("SELECT 1 FROM pg_catalog.pg_class \
       WHERE relkind = 'r' AND relname = '"+name+"' \
       AND pg_catalog.pg_table_is_visible(oid) LIMIT 1")
    return (rows.count>0)
  end
 
  def record_exists(table_name, title)
    rows = @db.exec("select id from "+table_name+" where title='"+title+"'")
    return (rows.count>0)
  end
 
  def show_all
    rows = @db.exec('select * from test')
    for col in rows.fields
      printf("%-15s", col)
    end 
    printf("\n---------------------\n")
    r = 0
    rows.each do |row|
      c = 0
      row.each do |col|
        printf("%-15s", rows.getvalue(r,c))
        c = c + 1
      end
      r = r + 1
      printf("\n")
    end
  end 

end

app = PgSQLTest.new
app.show_all
app.exit

Run it.
ruby pgsql01.rb
dbname: mydb, user: socrateos
id             title          
---------------------
1              test1          
2              test2  

Tuesday, August 31, 2010

Easy access to documentation of all gems installed

I had a difficulty finding a documentation for pg (ruby gem to access PostgreSQL).
Then I fond an easy way to access a documentation of any gem that I installed.

1. Just launch a web server:
gem server
And go to http://localhost:8808.
You have it all there.

More info here: http://docs.rubygems.org/read/chapter/18


2. Run it at startup.

a. Goto Preferences > Startup Applications.
b. Add new application:
- Name: gem server
- Command: gem server
c. Restart Ubuntu.

For other way of starting it up, see also:
http://lindsaar.net/2008/4/12/tip-2-daemonize-your-gems
http://embraceubuntu.com/2005/09/07/adding-a-startup-script-to-be-run-at-bootup/

Testing sqlite3-ruby

# sqlite01.rb

require 'rubygems'
require 'sqlite3'

class SQLiteTest

 def initialize
  @db = SQLite3::Database.new("test.db")
  @db.execute("create table test (id INTEGER PRIMARY KEY, title TEXT)")\
    unless(table_exists("test"))
  @db.execute("insert into test (title) values('test1')")\
    unless(record_exists("test", "test1"))
  @db.execute("insert into test (title) values('test2')")\
    unless(record_exists("test", "test2"))
 end
 
 def table_exists(name)
  rows = @db.execute("select name from sqlite_master where name='"+name+"'")
  return (rows.count>0)
 end
 
 def record_exists(table_name, title)
  rows = @db.execute("select id from "+table_name+" where title='"+title+"'")
  return (rows.count>0)
 end
 
 def show_all
  rows = @db.execute('select * from test')
  p rows
 end
 
end

db = SQLiteTest.new
db.show_all

The result:
$ ruby sqlite01.rb 
[["1", "test1"], ["2", "test2"]]

Monday, August 30, 2010

Installed Ruby gems for database

Here are the list of ruby gems for database access installed on my Ubuntu PC.
$ irb 
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'pg'
=> true
irb(main):003:0> require 'sqlite3'
=> true
irb(main):004:0> require 'sequel'
=> true
irb(main):005:0> quit
$ irb1.9.1 
irb(main):001:0> require 'pg'
=> true
irb(main):002:0> require 'sqlite3'
=> true
irb(main):003:0> require 'sequel'
=> true
irb(main):004:0> quit

Sunday, August 29, 2010

Ruby - require 'gtk2' error, fixed

I was getting error on my laptop (not desktop pc):
Gtk-MESSAGE **:Failed to load module "pk-gtk-module": libpk-gtk-module.so
every time when I try to require 'gtk2' in ruby.

I found a workaround suggested by LeeDaugherty at
http://ubuntuforums.org/showthread.php?t=1534719

It works.
1. open gconf-editor.
2. remove check mark from /apps/gnome_settings_daemon/gtk-modules/pk-gtk-module.

Uninstalling Netbeans 9,0beta

I uninstalled Netbeans 9.0beta from my laptop.

~$ cd netbeans-6.9beta/
$ sh uninstall.sh
Configuring the installer...
Searching for JVM on the system...
Extracting installation data...
Running the installer wizard...

Saturday, August 28, 2010

Installing sqlite3-ruby

[2011-08-21 OBSOLETE]

The ruby gem sqlite3-ruby has been renamed sqlite3.
The sqlite3-ruby is now just a dummy gem that depends on sqlite3.
(See here.)

See also my latest installation: "Installing SQLie3 for Ruby on Unbuntu 11.04"


1 Install libsqlite3 libraries.

a. Synaptic
b. Search and install
- libsqlite3-dev
- libsqllite3-ruby # ruby1.8
- libsqlite3-ruby1.9.1

2. Install ruby gem.
$ sudo gem install sqlite3-ruby
Building native extensions.  This could take a while...
Successfully installed sqlite3-ruby-1.3.1
1 gem installed
Installing ri documentation for sqlite3-ruby-1.3.1...
Enclosing class/module 'mSqlite3' for class Statement not known
No definition for libversion
Installing RDoc documentation for sqlite3-ruby-1.3.1...
Enclosing class/module 'mSqlite3' for class Statement not known
No definition for libversion
$ sudo gem1.9.1 install sqlite3-ruby
Building native extensions.  This could take a while...
Successfully installed sqlite3-ruby-1.3.1
1 gem installed
Installing ri documentation for sqlite3-ruby-1.3.1...
Updating class cache with 1723 classes...
Installing RDoc documentation for sqlite3-ruby-1.3.1...

3. Check the installation.
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):003:0> require 'sqlite3'
=> true
irb(main):004:0> quit
$ irb1.9.1 
irb(main):001:0> require 'sqlite3'
=> true
irb(main):002:0> quit

Installing sequel - a Ruby gem for database access

1. Install sequel
$ sudo gem1.9.1 install sequel 
Successfully installed sequel-3.14.0
1 gem installed
Installing ri documentation for sequel-3.14.0...
Updating class cache with 1382 classes...
Installing RDoc documentation for sequel-3.14.0...
$ sudo gem install sequel  # gem1.8
Successfully installed sequel-3.14.0
1 gem installed
Installing ri documentation for sequel-3.14.0...
Installing RDoc documentation for sequel-3.14.0...

2. Install PostgreSQL lib for Ruby
a. Synaptic
b. Search and install
- libpg-ruby
- libpg-ruby1.8
- libpg-ruby1.9.1
- libpg-ruby-doc

3. Install PostgreSQL dev version.
I am failing to install postgres/pg gem.
$ sudo gem install postgres
Building native extensions.  This could take a while...
ERROR:  Error installing postgres:
 ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb
extconf.rb:46: command not found: pg_config --includedir
extconf.rb:53: command not found: pg_config --libdir
checking for main() in -lpq... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
...
$ sudo gem install pg
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
 ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb
checking for pg_config... no
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
...

But I do not want to build PG from sources. So I install a dev version.
a. Synapitic
b. Find and Install postgresql-server-dev-8.4.

4. Now ready to install posgres/pg gem.
Actually I am confused of these two gems: postgres and pg.
I found posrgres gem fails to install for gem1.9.1, but pg installs on both versions, 1.8 and 1.9.1.

$ sudo gem install postgres
Building native extensions.  This could take a while...
Successfully installed postgres-0.7.9.2008.01.28
1 gem installed
Installing ri documentation for postgres-0.7.9.2008.01.28...
Installing RDoc documentation for postgres-0.7.9.2008.01.28...
$ sudo gem1.9.1 install postgres
Building native extensions.  This could take a while...
 ERROR:  Error installing postgres:
 ERROR: Failed to build gem native extension.

/usr/bin/ruby1.9.1 extconf.rb
extconf.rb:4:in : uninitialized constant PLATFORM (NameError)
$ sudo gem1.9.1 install pg
Building native extensions.  This could take a while...
Successfully installed pg-0.9.0
1 gem installed
Installing ri documentation for pg-0.9.0...
Updating class cache with 1720 classes...
Installing RDoc documentation for pg-0.9.0...
$ sudo gem install pg
Building native extensions.  This could take a while...
Successfully installed pg-0.9.0
1 gem installed
Installing ri documentation for pg-0.9.0...
Installing RDoc documentation for pg-0.9.0...

Friday, August 27, 2010

PostgreSQL Server Configuration

PostgreSQL Server configuration files are in /etc/postgresql/8.4/main/ directory.

$ locate postgresql
...
/etc/postgresql/8.4/main
/etc/postgresql/8.4/main/environment
/etc/postgresql/8.4/main/pg_ctl.conf
/etc/postgresql/8.4/main/pg_hba.conf
/etc/postgresql/8.4/main/pg_ident.conf
/etc/postgresql/8.4/main/postgresql.conf
/etc/postgresql/8.4/main/start.conf
...

The log file is at:
...
/var/log/postgresql
/var/log/postgresql/postgresql-8.4-main.log
...

I will make a change in conf file for log output.

1. Change to the configuration file directory:
$ cd /etc/postgresql/8.4/main/
$ ls -la
drwxr-xr-x 2 postgres postgres  4096 2010-08-24 07:59 .
drwxr-xr-x 3 postgres postgres  4096 2010-08-24 07:59 ..
-rw-r--r-- 1 postgres postgres   316 2010-08-24 07:59 environment
-rw-r--r-- 1 postgres postgres   143 2010-08-24 07:59 pg_ctl.conf
-rw-r----- 1 postgres postgres  3822 2010-08-24 07:59 pg_hba.conf
-rw-r----- 1 postgres postgres  1631 2010-08-24 07:59 pg_ident.conf
-rw-r--r-- 1 postgres postgres 16974 2010-08-24 07:59 postgresql.conf
-rw-r--r-- 1 postgres postgres   378 2010-08-24 07:59 start.conf

2. Make a backup copy of the original conf file.
Since postgresql.conf file is owned by postgres, I will make a copy with the same owner.
$ sudo -u postgres cp postgresql.conf postgresql.conf.bak.original
$ ls -la
drwxr-xr-x 2 postgres postgres  4096 2010-08-27 22:24 .
drwxr-xr-x 3 postgres postgres  4096 2010-08-24 07:59 ..
-rw-r--r-- 1 postgres postgres   316 2010-08-24 07:59 environment
-rw-r--r-- 1 postgres postgres   143 2010-08-24 07:59 pg_ctl.conf
-rw-r----- 1 postgres postgres  3822 2010-08-24 07:59 pg_hba.conf
-rw-r----- 1 postgres postgres  1631 2010-08-24 07:59 pg_ident.conf
-rw-r--r-- 1 postgres postgres 16974 2010-08-24 07:59 postgresql.conf
-rw-r--r-- 1 postgres postgres 16974 2010-08-27 22:24 postgresql.conf.bak.original
-rw-r--r-- 1 postgres postgres   378 2010-08-24 07:59 start.conf

3. Make a change.
The following changes are made base on suggestion by Suzuki Koichi at
http://lets.postgresql.jp/documents/tutorial/ubuntu/3

#-------------------------------------------------
# ERROR REPORTING AND LOGGING
#-------------------------------------------------
log_destination = 'stderr'       # 2010.08.27 - enabled
logging_collector = on           # 2010.08.27 - enabled and turned on
log_directory = 'pg_log'         # 2010.08.27 - enabled (I will create)
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # 2010.08.27 - enabled
log_truncate_on_rotation = off   # 2010.08.27 - enabled, keep it off
log_rotation_age = 1d            # 2010.08.27 - enabled
log_rotation_size = 10MB         # 2010.08.27 - enabled

4. Now create a new directory (pg_log) for log outputs.
$ su postgres 
$ cd ~/8.4/main/
$ mkdir pg_log
$ ls -F
PG_VERSION  global/  pg_log/  ....

5. Finally, restart the server.
$ sudo /etc/init.d/postgresql-8.4 restart
 * Restarting PostgreSQL 8.4 database server  

6. Confrim the log output.
$ sudo ls /var/lib/postgresql/8.4/main/pg_log
postgresql-2010-08-27_230554.log