F
Felipe Balbi
Hi all,
To automate some of the tests I have to run, I decided to use ruby to
generate some script files on a particular (very simple) language based
on several possible input files. My first approach at this was to use
inherited method on a parent class (which I called InputFormat) to hold
all the children in an array. Then, different formats could become a
child class of InputFormat and return a known data format (I decided to
use an array of hashes because the output is really really simple) to
the output generator code.
So the idea is something like:
class InputFormat
@children = []
def initialize(input)
@input = input
end
def parse
@children.each { |child|
child.parse(@input) if child.supported?(@input)
}
end
def self.inherited(child)
@children << child
end
end
class AInputFormat < InputFormat
def supported?
# check if we can parse this type of file
end
def parse
# parse and generate array of hashes in known format
end
end
Then on the core file I would have something like:
input = InputFormat.new(ARGV[0])
input.parse
As it turns out, this isn't working because AInputFormat will only
inherit from InputFormat at the time I actually use it, am I right ? Any
tips you guys could give me to achieve what I want ? (from several
possible input formats generate one output format)
To automate some of the tests I have to run, I decided to use ruby to
generate some script files on a particular (very simple) language based
on several possible input files. My first approach at this was to use
inherited method on a parent class (which I called InputFormat) to hold
all the children in an array. Then, different formats could become a
child class of InputFormat and return a known data format (I decided to
use an array of hashes because the output is really really simple) to
the output generator code.
So the idea is something like:
class InputFormat
@children = []
def initialize(input)
@input = input
end
def parse
@children.each { |child|
child.parse(@input) if child.supported?(@input)
}
end
def self.inherited(child)
@children << child
end
end
class AInputFormat < InputFormat
def supported?
# check if we can parse this type of file
end
def parse
# parse and generate array of hashes in known format
end
end
Then on the core file I would have something like:
input = InputFormat.new(ARGV[0])
input.parse
As it turns out, this isn't working because AInputFormat will only
inherit from InputFormat at the time I actually use it, am I right ? Any
tips you guys could give me to achieve what I want ? (from several
possible input formats generate one output format)