I have a class with a constructor that takes two parameters and sets the fields to those values. I want to also be able to initialize objects from this class by setting fields explicitly inside of a block passed to the constructor.
The constructor:
class Grammar
attr_reader :rules, :start, ...
def initialize(rules, start)
@rules = rules
@start = start
...
end
end
The parameters passed to the constructor involve creating several objects that are only used as intermediate building blocks for the parameters rules and start and it would make sense to limit the existence of these objects to the block passed to the constructor. In this block I would expect to first build the intermediate objects and then directly set the fields of the new object.
I want to be able to instantiate Grammar like so:
grammar = Grammar.new { |g|
...
# build rules and start
...
g.start = start
g.rules = rules
}
How should I modify the constructor to allow both methods of initialisation?