From: Mark Hubbart Date: 2004-04-22T03:58:55+09:00 Subject: Re: yaml doesn't save Proc members On Apr 21, 2004, at 8:59 AM, Dick Davies wrote: > > I'm twiddling around with a dependency tree implementation - > a bit like rake, but for things like keeping track of what servers > need shutting down if spamassassin has to bounce for some reason. > > It's still very theoretical, but effectively you have a node which > has > > : a name (symbol) > : a dependency list (array of nodes) > : a 'users' list (array of nodes which depend on *it*) > : a block (which gets called to 'resolve' this dependency > > I thought I'd bugger about with YAML to save and load the tree, > but although the arrays all get laid down nicely, the blocks don't. Ruby pretty much discards the code once it has been compiled; there is no way to get it back without re-parsing the script and selecting out the original definition. I would suggest that you might want to store the block/proc as a string form the beginning, and eval it when you want the actual block: block = "{|n| n * 3 }" (0..5).map &eval("proc " + block) that way you can store the block in a yaml document, albeit as a string. > When I call to_yaml, the proc is saved as an empty block, > and when YAML.load() tries to pull it back, I get: > > NoMethodError: allocator undefined for Proc > /data/ruby/lib/ruby/1.9/yaml.rb:171:in `load' > /data/ruby/lib/ruby/1.9/yaml.rb:171:in `object_maker' > /data/ruby/lib/ruby/1.9/yaml/rubytypes.rb:36 > /data/ruby/lib/ruby/1.9/yaml/rubytypes.rb:34:in `call' > /data/ruby/lib/ruby/1.9/yaml.rb:39:in `load' > /data/ruby/lib/ruby/1.9/yaml.rb:39:in `load' > test/fuct_test_knot_yaml.rb:55:in `test_yaml_block' > > So I guess I need another way to attach behaviour to a node... > would singleton methods have the same problem ? > I'm not big on inheritance.... > > -- > I don't like spinach, and I'm glad I don't, because if I > liked it I'd eat it, and I just hate it. > -- Clarence Darrow > Rasputin :: Jack of All Trades - Master of Nuns >