Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
[QUIZ] SerializableProc (#38
James Gray
7/8/2005 7:18:00 PM
The three rules of Ruby Quiz:
1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.
2. Support Ruby Quiz by submitting ideas as often as you can:
http://www.rub...
3. Enjoy!
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
I'm a Proc addict. I use them all over the place in my code. Because of that,
whenever I end up needing persistence and I call Marshal.dump() or YAML.dump()
on some object hierarchy, I get to watch everything explode (since Procs cannot
be serialized).
This week's Ruby Quiz is to build a Proc that can be serialized.
I'm not aware of any possible way to add serialization capabilities to Ruby's
core Proc, which rules out a complete solution. However, even if what we build
is a hack, at least one person finds it super useful.
The task then is to build SerializableProc. It should support being serialized
by Marshal, PStore, and YAML and otherwise behave as close to a Proc as
possible. Put another way, make the following code run for your creation:
require "pstore"
require "yaml"
code = # Build your SerializableProc here!
File.open("proc.marshalled", "w") { |file| Marshal.dump(code, file) }
code = File.open("proc.marshalled") { |file| Marshal.load(file) }
code.call
store = PStore.new("proc.pstore")
store.transaction do
store["proc"] = code
end
store.transaction do
code = store["proc"]
end
code.call
File.open("proc.yaml", "w") { |file| YAML.dump(code, file) }
code = File.open("proc.yaml") { |file| YAML.load(file) }
code.call
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
[QUIZ] SerializableProc (#38
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password