From: Robert Klemme Date: 2008-05-20T16:23:01+09:00 Subject: Re: how to serialize hash to a file? 2008/5/20 Joel VanderWerf : > hurcan solter wrote: >> >> i have an xml file that goes like; >> >> >> >> >> >> >> at some point i need the look up the data type for different >> attributes. >> I can get the data type using a Xpath query in the form using REXML >> >> node = $fom_file.elements["//attribute [@name = '#{attribute_name}']"] >> >> it works okay, the thing is ,it takes ages (which is about ten >> seconds) >> >> so I have a mind to cache the attribute-datatype pairs in a hash for >> faster lookup. >> So can you guys tell me a simple and elegant way to do serialize >> deserialize that hash to a file? >> TIA >> Hurcan Solter > > Pretty easy... > > h = {1=>2, 3=>4} > File.open("foo", "wb") {|f| Marshal.dump(h, f)} > p File.open("foo", "rb") {|f| Marshal.load(f)} # ==> {1=>2, 3=>4} Amazing how we nearly picked the same solution - even the file name is identical. :-) Cheers robert -- use.inject do |as, often| as.you_can - without end