Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login

tut-gconf

Ruby/GConf2 Tutorial

GConf is a configuration data storage mechanism. It's one of the easiest way to store configuration data. And its features are so cool and elegant. If you are thinking of adding customization functionality to your application, check it out!

See Introduction to the GConf library. It's a nice introduction for GConf.

First step

This is an easy but complete sample.

require 'gconf2'
client = GConf::Client.new

#Read data
p client["/apps/hoge/stage"]
p client["/apps/hoge/name"] 

#Write data
client["/apps/hoge/stage"] = 1
client["/apps/hoge/name"] = ARGV[0]

Save this sample as "test.rb" and execute twice.

$ruby test.rb "hoge"
$ruby test.rb "fuga"

At first, it returns nils because no data is stored.

Second time around, it returns 1 and "hoge".

# If you execute it once more, you get 1 and "fuga".

That's all! You do nothing anymore. You can serialize the data and read/write data anytime!

Where's the data stored?

Data is managed by gconfd, which is a per-user daemon. And they are saved to $HOME/.gconf/apps/hoge/%gconf.xml asynchronously. In this case, both of "/apps/hoge/stage" and "/apps/hoge/name" are keys. And the values are String, Numeric, true/false.

Notification of changes

GConf::Client uses the GTK signal system. Instead of using GConf's custom callback API for notification, you can simply connect to a "value_changed" signal. Try to follow sample in 2 terminal and execute them on each terminal.

require 'gconf2'

client = GConf::Client.new
client.add_dir("/apps/hoge")
client.notify_add("/apps/hoge/name") do |client, entry|
  p entry.value
end

Gtk.main

ChangeLog

2003-09-11 lone-star
Small language fixes.
2003-09-11 Masao
Translate to English.
2003-06-19 Masao
Initial release(Japanese)
Last modified:2011/06/11 10:11:34
Keyword(s):
References:[tutorials]