Well, by 'clumsy' I meant exactly something like
> login(Options['server'], Options['port'], Options['user'], Options['pass'])
and so on throughout the script.
Hmm, I don't see that as significantly different from:
login(SERVER, PORT, USER, PASS)
Though I will concede that it's a little shorter.
I would probably pass some kind of options object into the method and query it from inside:
#!/usr/local/bin/ruby -w
require "ostruct"
require "yaml"
def open_connection( connection )
p connection.server
end
if File.exists? "config.yaml"
config = File.open("config.yaml") { |file| YAML.load(file) }
open_connection(config)
else
config = OpenStruct.new( :server => "news.example.org",
:port => 100,
:user => "myname",
:pass => "topsecret" )
File.open("config.yaml", "w") { |file| YAML.dump(config, file) }
end
__END__
Even better, perhaps a Connection object could manage its own data, and then you could just YAML it back and forth, or something similar.
I don't know what reflection means in this context, but am I guessing
right that we are talking about something (maybe something more elegant)
that has the same *effect* as:
def getOptions
Options = YAML.load_file(OPTFILE)
SERVER = Options['server']
PORT = Options['port']
...
end
Here's what I basically had in mind:
#!/usr/local/bin/ruby -w
require "yaml"
$standard_constants = Module.constants
def save_config
config = Hash.new
(Module.constants - $standard_constants).each do |const|
config[const] = Module.const_get(const)
end
File.open("config.yaml", "w") { |file| YAML.dump(config, file) }
end
def load_config
config = File.open("config.yaml") { |file| YAML.load(file) }
config.each_pair do |const, value|
Object.const_set(const, value)
end
end
if File.exists? "config.yaml"
load_config
p SERVER
else
SERVER = "news.example.org"
PORT = 100
USER = "myname"
PASS = "topsecret"
save_config
end
__END__
Hopefully this gives you some fresh ideas.
James Edward Gray II
···
On Sep 6, 2005, at 1:51 PM, Oliver Cromm wrote: