I needed a class that acted like a container, as well as doing other things.
I tried making it a subclass of Array, instead of adding an Array member. It
was working fine until I tried to reload it from YAML. Here is some code
that reproduces the problem. It seems like the Size parameter is not being
output in the YAML string.
- Why doesn't it work?
- How can I make it work?
···
----
require 'Yaml'
class Box< Array
def initialize size
@size = size
end
def size
@size
end
end
box = Box.new (2)
box << "orange"
box_yaml = box.to_yaml
p box_yaml
# prints "--- !ruby/array:Box \n- orange"
box2 = YAML::load(box_yaml)
# produces:
=============== ArgumentError =====================
c:\ruby\lib\ruby\1.8/yaml/rubytypes.rb:239:in `initialize'
o = obj_class.new
c:\ruby\lib\ruby\1.8/yaml/rubytypes.rb:239:in `new'
o = obj_class.new
c:\ruby\lib\ruby\1.8/yaml/rubytypes.rb:239
o = obj_class.new
c:\ruby\lib\ruby\1.8/yaml/rubytypes.rb:235:in `call'
array_proc = Proc.new { |type, val|
c:\ruby\lib\ruby\1.8/Yaml.rb:119:in `transfer'
yp = @@parser.new.load( io )
c:\ruby\lib\ruby\1.8/Yaml.rb:119:in `load'
yp = @@parser.new.load( io )
c:\ruby\lib\ruby\1.8/Yaml.rb:119:in `load'
yp = @@parser.new.load( io )
c:\ruby\lib\ruby\1.8\test.rb:21
box2 = YAML::load(box_yaml)
c:\ruby\lib\ruby\site_ruby\1.8/rubygems/custom_require.rb:18:in `require__'
require__ path
c:\ruby\lib\ruby\site_ruby\1.8/rubygems/custom_require.rb:18:in `require'
require__ path
=============================================
Exception: wrong number of arguments (0 for 1)
Thanks,
-Adam