Read and store definitions from an outer file

Hi guys, I am a newcomer to Ruby, i got into it by having to make
testing scripts for my company. I have not had such problem that i
couldn’t solve by reading, but now I encountered one. I have made some
test suites using watir gem which include definitons like user name and
test page address. But if i move to an other environment i have to
modify these for each test suite. What i would like to achieve: I would
like to have a single file(testreq.txt for example) containing these
definitions and I would like to make my test suites to read and store
these values from my testreq.txt file. Thank you very much for your help
in advance!

Best wishes,
Alex

On Jul 10, 2012, at 7:34 AM, firstsense sdasad [email protected]
wrote:

Best wishes,
Alex


Posted via http://www.ruby-forum.com/.

Hey,

I normally use a module filled with constants.

Module foo
PropertyOne=“some value”
End

Then just require the module from somewhere else.

If you have complex config needs, try serialising data structures to and
from Yaml.

Jams

Hi,

Using a Module as Jam suggested is probably the simplest way.

But of course there are also many data formats you can use. I actually
prefer JSON over YAML, because it’s simpler and more “robust”. Reading
and writing works pretty much like with YAML:

require ‘json’

config_file = ‘config.json’

write

new_config = {
name: ‘some name’,
address: ‘some address’
}
File.open(‘config.json’, ‘w’) {|f| f << new_config.to_json}

#read
current_config = JSON.parse File.read config_file
puts current_config

Am 10.07.2012 18:29, schrieb Jan E.:

Hi,

Using a Module as Jam suggested is probably the simplest way.

But of course there are also many data formats you can use. I actually
prefer JSON over YAML, because it’s simpler and more “robust”. Reading
and writing works pretty much like with YAML:

For a config file that I or (somebody else) might want to edit
directly, I find YAML more human readable than JSON

JSON:
{“name”:“some name”,“address”:“some address”}

YAML:
:name: some name
:address: some address

But, of course, that’s a matter of taste.

Am 10.07.2012 15:34, schrieb firstsense sdasad:

Best wishes,
Alex

you could use YAML:

require ‘yaml’

config_file = ‘testreq.txt’

config = {:name => ‘User’, :address => ‘Address’}
File.open(config_file, ‘w’) {|f| f.write(config.to_yaml) }

or: File.open(config_file, ‘w’) {|f| YAML.dump(config, f) }

saved_config = YAML::load_file(config_file)
puts “Name: #{saved_config[:name]}” #=> Name: User

Thank you very much guys. I managed to make everything work w/ your
info! <3