Closed jvalrog closed 4 years ago
When you subclass ROS, it uses your subclass for the children instances. Since you changed the method signature of initialize
(caught because the arity changed from 2 to 1), it failed when it tried to create a new Config
instance internally (why the exception happened on config.rb:5
where you override initialize
)
You probably want to use composition and not a subclass that changes the method signature of initialize
. Either create a function that accepts a filename and returns an ROS, or create another type called Config
that has an accessor method that returns the ROS data structure.
class Config
def initialize(filename)
@filename = filename
@config = RecursiveOpenStruct.new(YAML.load_file(@filename), {recurse_over_arrays: true, preserve_original_keys: true})
end
attr_accessor :config
end
my_config = Config.new("config.yml")
my_config.config
# => returns the ROS
my_config.config.devices
# => should return the devices sub-ROS
If you don't want the extra accessor in there, you could use a delegator pattern so that your Config
forwards messages it doesn't implement on to the ROS structure.
Hello, thanks for this gem, it's great. I'm trying this code but for some reason it doesn't work:
The result is this:
If I use OpenStruct instead of RecursiveOpenStruct, the code works (but it's not recursive).
Apparently, the first "puts" works, but not the other one. Am I missing something? It's been a while from my coder days.
Thanks