In my initialize method I am trying to write it so I can pass either a hash or a yaml object to init the attribute values.
My yaml file looks like:
defaults: &defaults
host: localhost
port: 4565
timeout: 3
development:
<<: *defaults
test:
<<: *defaults
staging:
<<: *defaults
production:
<<: *defaults
I currently have this:
def initialize(options)
if options.respond_to? "has_key" && options.has_key? "defaults"
config = options["defaults"]
else
config = options
end
@hostname = config[:hostname]
@port = config[:port]
@timeout = config[:timeout]
end
This is currently not working for me, I’m getting this error:
unexpected tSTRING_BEG, expecting keyword_then or ';' or '\n'
if options.respond_to? "has_key" && options.has_key? "defaults"
- How can I load the correct environment also? (test, development, production)
- How can I throw an error if one of the keys isn’t present? (or say at least the major ones that I need for sure like hostname, port)
You need to parenthesize your if statement.