R
Robin Stocker
Hi,
This is the second solution that I could finish in time. Well, it was
pretty easy.
I imagine my solution is not very fast, as each time a method on the
SerializableProc is called, a new Proc object is created.
The object could be saved in an instance variable @proc so that speed is
only low on the first execution. But that would require the definition
of custom dump methods for each Dumper so that it would not attempt to
dump @proc.
Here's my solution...
(Question: Is it better if I attach it or just paste it like this?)
class SerializableProc
def initialize( block )
@block = block
# Test if block is valid.
to_proc
end
def to_proc
# Raises exception if block isn't valid, e.g. SyntaxError.
eval "Proc.new{ #{@block} }"
end
def method_missing( *args )
to_proc.send( *args )
end
end
if $0 == __FILE__
require 'yaml'
require 'pstore'
code = SerializableProc.new %q{ |a,b| [b,a] }
# Marshal
File.open('proc.marshalled', 'w') { |file| Marshal.dump(code, file) }
code = File.open('proc.marshalled') { |file| Marshal.load(file) }
p code.call( 1, 2 )
# PStore
store = PStore.new('proc.pstore')
store.transaction do
store['proc'] = code
end
store.transaction do
code = store['proc']
end
p code.call( 1, 2 )
# YAML
File.open('proc.yaml', 'w') { |file| YAML.dump(code, file) }
code = File.open('proc.yaml') { |file| YAML.load(file) }
p code.call( 1, 2 )
p code.arity
end
This is the second solution that I could finish in time. Well, it was
pretty easy.
I imagine my solution is not very fast, as each time a method on the
SerializableProc is called, a new Proc object is created.
The object could be saved in an instance variable @proc so that speed is
only low on the first execution. But that would require the definition
of custom dump methods for each Dumper so that it would not attempt to
dump @proc.
Here's my solution...
(Question: Is it better if I attach it or just paste it like this?)
class SerializableProc
def initialize( block )
@block = block
# Test if block is valid.
to_proc
end
def to_proc
# Raises exception if block isn't valid, e.g. SyntaxError.
eval "Proc.new{ #{@block} }"
end
def method_missing( *args )
to_proc.send( *args )
end
end
if $0 == __FILE__
require 'yaml'
require 'pstore'
code = SerializableProc.new %q{ |a,b| [b,a] }
# Marshal
File.open('proc.marshalled', 'w') { |file| Marshal.dump(code, file) }
code = File.open('proc.marshalled') { |file| Marshal.load(file) }
p code.call( 1, 2 )
# PStore
store = PStore.new('proc.pstore')
store.transaction do
store['proc'] = code
end
store.transaction do
code = store['proc']
end
p code.call( 1, 2 )
# YAML
File.open('proc.yaml', 'w') { |file| YAML.dump(code, file) }
code = File.open('proc.yaml') { |file| YAML.load(file) }
p code.call( 1, 2 )
p code.arity
end