Chris C. and I threw together a simple gem to help create nested
hierarchies (without relying on constructors). Sample usage:
car = Car.lay( :color => “Red”, :driver => Driver.lay( :name => “Bob”
Is anyone aware of something out there that already does this (in Ruby)?
Note that in the above example Car and Driver might not have
defined. As you’ve probably guessed the example simply instantiates a
sets its ‘color’ attr to “Red”, and sets its ‘driver’ attr to a new
instance (whose ‘name’ attr is set to “Bob”).
The idea was largely borrowed from groovy. I’ve used the groovy way of
doing this extensively and have found it to be particularly useful in
cases, when you need to construct nested objects to use as test input.
Please let us know if we’re reinventing the wheel. Otherwise, please do
“gem install lay” and try it out and give us feedback if you think it
be useful to you.
- Also pass parameters to constructors
- Car.lay( [“Jetta”, “V6”], :color => “Red” )
- Set member vars that don’t have attr’s defined
- Car.lay( :_color => “Red” )
- This sets the @color member variable
- We should probably change it to use :@color instead of :_color