Twitter Updates

Sunday, 22 March 2009

Ruby finding OS

To find the Operating System that you ruby script/program is running on you can do:
puts RUBY_PLATFORM

To return one of Windows, Mac, Linux you could do:
if (RUBY_PLATFORM.include? ("darwin"))
os = "Mac"
elsif (RUBY_PLATFORM.include? ("nix"))
os = "Linux"
elsif (RUBY_PLATFORM.include? ("Win"))
os = "Windows"
else
os = ""
end

there is also
puts ENV['OS']
but this only works on windows and is empty for Linux and Mac OS X

Saturday, 21 March 2009

Ruby Shoes defining centre point of text

I figured out this method for defining the centre point of a block of text. Actually you define the start and width of a flow element of which text can be centred in.
#Centred Text
@app.flow (:left => 50, :width => 60) {
para ("Centered Text", :align => "center")
}

Friday, 20 March 2009

Ruby Shoes Tabs Layout

I have just released a very basic framework for putting tabs across the top of a Ruby shoes window.

ShoesTabsLayout

Ruby Shoes Positioning Text

Ruby Shoes Positioning text and links can be done like this:
@app.para ("Text to Position", :left => 100 )
@app.para (link("main", :click=>"/", :stroke => black), :left => 200 )

Ruby Shoes Link nounderline

I currently find it hard to find short cookbook style examples of Ruby Shoes so I will probably be posting lots of small snippets here for me to refer back to.

First up creating a link for a multipage (not multi-window) application with out the link and black text:
link("main",  :click=>"/",       :stroke => black, :underline => "none")

Thursday, 19 March 2009

Rubys update method

In my last attempt at a config system for ruby GUI apps I wanted to define default values for the preference options, so that if it gets expanded with new values it does not break when used with old config files.

I tried something like:
def loadConfig(configFile)
config = {}
config[:raw_conf_folder_loc] = "1"
config[:drv_conf_folder_loc] = "2"
if File.exist?(configFile)
config = open(configFile) {|f| YAML.load(f) }
end
end


This creates a config object (an array of hashes) and assign some values. Then if the config file exists move the pointer to a new object loaded from file, which then leaves the old config object to be picked up by the garbage collector. We are only left with the options in the file defined any defaults set will readback as nil.

To get around this problem I did the following:
def loadConfig(configFile)
if File.exist?(configFile)
config.update(open(configFile) {|f| YAML.load(f) })
end
#do this to set parameters that might be missing from the yaml file
if config[:raw_conf_folder_loc].nil?
config[:raw_conf_folder_loc] = "1"
end
if config[:drv_conf_folder_loc].nil?
config[:drv_conf_folder_loc] = "2"
end
return config
end

But this is an extra 3 lines per option and looks ugly.
Today I discovered a better way of doing this using the update() method.

def loadConfig(configFile)
config = {}
config[:raw_conf_folder_loc] = "1"
config[:drv_conf_folder_loc] = "2"
if File.exist?(configFile)
config.update(open(configFile) {|f| YAML.load(f) })
end
return config
end


Just like the first version but using update() instead of =, this replaces the default values that have been assigned inside the file. Full example ruby_yaml_config.rb

Tuesday, 17 March 2009

A Better Ruby preference system using YAML

An improved preference system for ruby, which can also give an empty string for new values.
ruby_yaml_config.rb

Based on this