That's one way to do it ... Another is to use YAML which has a pretty
heavy following with the Ruby community in general...

So, the following would do the same ... 

You can simply use a hash to store things... Here's a quick script to
load up a sample

--SCRIPT--
require 'yaml'

config = Hash.new
config["username"] = "joe"
config["password"] = "pass"
config["foo"] = "bar"
config["baz"] = 5
config["zoo"] = [ 1, 2, 3, 4, 5 ]

File.open( "config.yaml", "w" ) { |file| file << config.to_yaml }
--SCRIPT--

Then in your normal script you can get to this as simply as: 

--SCRIPT--

require 'yaml'

config = YAML.load( File.new( 'config.yaml' ) )

config.each_pair { |key,value| puts "#{ key } -- #{ value }" }
--SCRIPT--

It's that easy ... YAML is pretty cool.

Hope that helps.

j.

On 8/29/05, Kingsley <[EMAIL PROTECTED]> wrote:
>  
>  
> 
> Hi 
> 
>   
> 
> You might find this useful: 
> 
>   
> 
> class MissingConfigurationFile < ArgumentError ; end 
> 
>   
> 
> class MyConfig 
> 
>   
> 
>     MyConfig = Struct.new("MyConfig", :property, :value) 
> 
>   
> 
>     def initialize(config_file) 
> 
>         begin 
> 
>             @config = File.readlines(config_file) 
> 
>         rescue 
> 
>             raise MissingConfigurationFile, "Can't find config file" 
> 
>         end 
> 
>     end 
> 
>   
> 
>     def parse_config_file 
> 
>         rem_array = [] 
> 
>         prop_array = [] 
> 
>         @config.each do |line| 
> 
>             if line.match(/^#/) 
> 
>                 rem_array << line 
> 
>             else 
> 
>                 unless line.match(/^\n$/) then prop_array << line.chomp end 
> 
>             end 
> 
>         end 
> 
>         return prop_array - rem_array 
> 
>     end 
> 
>   
> 
>     def properties 
> 
>         config_hash = {} 
> 
>         parse_config_file.each do |data| 
> 
>             split = data.split("=") 
> 
>             property = split[0].gsub(/\s*/, "") 
> 
>             value = split[1].gsub(/$\s*/, "").gsub(/^\s*/, "") 
> 
>             config_hash.store(property,MyConfig.new(property, value)) 
> 
>         end 
> 
>         return config_hash 
> 
>     end 
> 
>             
> 
> end 
> 
>   
> 
> myConfig = MyConfig.new("config.txt").properties 
> 
> config_option_name = myConfig['OPTION_NAME].property 
> 
> config_option_value = myConfig['OPTION_NAME'].value 
> 
>   
> 
> config.txt 
> 
>   
> 
> # Lines with a # are ignored 
> 
> OPTION_NAME = put some value here 
> 
> OPTION_2 = more options 
> 
>   
> 
> Hope it's useful 
> 
>   
> 
> Thanks 
> 
>   
> 
> Kingsley 
> 
>   
> 
> -----Original Message-----
>  From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of
> [EMAIL PROTECTED]
>  Sent: 29 August 2005 06:40
>  To: wtr-general@rubyforge.org
>  Subject: [Wtr-general] Key / value pairs. 
> 
>   
> 
> This email is to be read subject to the disclaimer below.
>  
>  
>  Hello all, 
>  
>  I'm new to Watirr (and Ruby), so forgive me if the question is rather
> simplistic... 
>  
>  What I'd like to do, is to have a configuration file (eg. config.txt),
> which would have a number of key/value pairs, such as "UserName=user", which
> would be read by a test script to, for example, log into a page. Hence, what
> I need, is a method to read each line in a given file; strip off the
> comments (if exist; denoted by #); find the pair based on the key (ie.
> UserName) and get the value (ie. user) and store it in a variable. 
>  
>  Any help would be greatly appreciated. 
>  
>  Regards 
>  
>  Leon 
>  
>  --------------------
>  NOTICE - This communication contains information which is confidential and
> the copyright of Ernst & Young or a third party. 
>  
>  If you are not the intended recipient of this communication please delete
> and destroy all copies and telephone Ernst & Young on 1800 655 717
> immediately. If you are the intended recipient of this communication you
> should not copy, disclose or distribute this communication without the
> authority of Ernst & Young.
>  
>  Any views expressed in this Communication are those of the individual
> sender, except where the sender specifically states them to be the views of
> Ernst & Young.
>  
>  Except as required at law, Ernst & Young does not represent, warrant and/or
> guarantee that the integrity of this communication has been maintained nor
> that the communication is free of errors, virus, interception or
> interference.
>  
>  Liability limited by a scheme approved under Professional Standards
> Legislation.
>  --------------------
>  
>  
>  If this communication is a "commercial electronic message" (as defined in
> the Spam Act 2003) and you do not wish to receive communications such as
> this, please forward this communication to [EMAIL PROTECTED] 
> _______________________________________________
> Wtr-general mailing list
> Wtr-general@rubyforge.org
> http://rubyforge.org/mailman/listinfo/wtr-general
> 
> 
> 


-- 
"So long, and thanks for all the fish"

Jeff Wood

_______________________________________________
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general

Reply via email to